Tag Info

Hot answers tagged

24

Since you use brace expansion anyway so make use of its feature fully: echo {375..3500..5} To print each number in separate line with optional text: printf "Number %s is generated.\n" {375..3500..5} Number 375 is generated. Number 380 is generated. Number 385 is generated. ... Edit As pointed out by @kojiro in the comment Mac OS uses bash 3 as the ...


18

Alternatively a traditional C-style for loop can be used: for ((i=375; i<=3500; i+=5)); do echo $i done This is perhaps less clear than using seq, but it doesn't spawn any subprocesses. Though since I'm familiar with C, I wouldn't have any difficulty understanding this, but YMMV.


15

Using SEQ(1) for i in $(seq 375 5 3500) do echo $i done Or, simply: seq 375 5 3500


13

The function returns, but the command substitution blocks, because you created a background job, but you still have your stdout fd opened. Just close it by adding >/dev/null before the &. #!/bin/bash function start { leafpad >/dev/null & echo $! } PID=$(start) echo "PID is $PID" If you want your process to have also stdin, stdout, stderr ...


11

Your for-loop snippet didn't work as you require for two reasons: (($i += 5)) - here the $i is expanded to the value of i. Thus the expansion will be something like ((375 += 5)), which doesn't make sense (attempting to assign a literal number to another literal number). This would normally be achieved with ((i += 5)) (no $ to expand the variable) The ...


10

In your case parenthesis () are used as array definition, for example a=(one two three) # array definition echo "${a[1]}" # print *second* element of array a echo "${#a[@]}" # print number of elements in array a If you put single variable in array then you just have an array with single element. To answer your other question whether ...


10

GNU seq takes separator (-s) option : $ seq -s ' ' 1 5 1 2 3 4 5 $ var="$(seq -s ' ' 1 5)" $ echo "$var" 1 2 3 4 5


9

First check if the path to add is already part of the variable: [[ ":$PATH:" != *":/path/to/add:"* ]] && PATH="/path/to/add:${PATH}" If /path/to/add is already in the $PATH, then nothing happens, else it is added at the beginning. If you need it at the end use PATH=${PATH}:/path/to/add instead. Edit: In you case it would look like this: [[ ...


8

bash doesn't have multi-dimensional arrays yet. Only ksh93 does. Here, you can use a csv-like structure and you don't even need to use arrays: #! /bin/sh - gws="\ foo,1.1.1.12,1::1 blah,2.2.2.2,2::2" while IFS=, read name ip4 ip6; do echo something with "$name" "$ip4"... done << E $gws E (you don't even need bash) With ksh93: gws=( (name=foo ...


8

Using GNU date : $ date -d '2015-09-26 +30 days' '+%Y-%m-%d' 2015-10-26


7

That's because you are checking whether the string gpio -g read 22 is greater than 1. Since gpio -g read 22 is not a number, you get that error. You don't explain what you are trying to do but I'm guessing you want to compare the output of the gpio command. To do that, you need to enclose the command in $() or backticks (``): x=$(gpio -g read 22) if [ ...


7

With bash, zsh and ksh93, you can use {start..end..step} brace expansion form: $ echo {1..10} 1 2 3 4 5 6 7 8 9 10


7

a for loop: for f in *.c; do cp -- "$f" "$OTHERDIR/old#$f"; done I often add the -v option to cp to allow me to watch the progress.


6

man login BUGS A recursive login, as used to be possible in the good old days, no longer works; for most purposes su(1) is a satisfactory substitute. Indeed, for security reasons, login does a vhangup() system call to remove any possible listening processes on the tty. This is to avoid password sniffing. If one uses the command ...


5

Edited following OP's clarification on the use case: You can not do that using the latest official release of bash (currently 4.3.30 according to this page). lib/sh/netopen.c shows that bash opens a UDP socket (SOCK_DGRAM) then directly tries to connect without looking at the ip address to determine whether it would make sense to set specific socket options ...


5

The sysadmin in my old lab had a nifty little function for this: pathmunge () { if ! echo "$PATH" | /bin/grep -Eq "(^|:)$1($|:)" ; then if [ "$2" = "after" ] ; then PATH="$PATH:$1" else PATH="$1:$PATH" fi fi } It will both check whether the string given is already in the PATH and ...


5

While there is, of course, an app for that (seq 375 5 3500), there are various ways of doing this from the commandline. While the fastest and simplest will be just using seq, here are some other options: for i in {375..3500}; do [[ (($i % 5)) -eq 0 ]] && echo $i; done i=370; while [ $i -le 3500 ]; do printf "%s\n" $((i+=5)); done perl -le ...


5

The argument to -c must be a single word, so su administrador -c "$OPENP_DIR/ctlscript.sh start" For "restart", you should "stop" first, then "start"


4

POSIXly: i=370 while [ 3500 -gt "$i" ] do echo "$((i+=5))" done ...or... echo 'for(x=370;x<=3500;x+=5)x' |bc I dunno why you'd do it any other way. Excepting, of course... seq 375 5 3500 ...or with dc: echo '370[5+pd3500>p]splpx'|dc


4

You can use the DEBUG trap to do this. In this trap, $BASH_COMMAND contains the command last executed. trap 'echo "you tried to call the command [$BASH_COMMAND]"' DEBUG Note that, if you are executing commands as part of your prompt or $PROMPT_COMMAND, the trap will run on these as well. You can add checks to see if $BASH_COMMAND is the same as ...


4

Look into your ~/.bashrc or ~/.profile, there may be a commented prompt setup that should do what you want, like this one on our infra: export PS1='\h:\w\$ ' Which looks like: coolservername:~# Or if you plan on logging as non-root, you can use: export PS1='\u@\h:\w\$ ' to add username before the hostname. You can have fun adding colours, ...


4

Add these lines to your .bashrc: prompt_on_top() { tput cup 0 0 tput el tput el1 } pre_cmd() { if [ "$BASH_COMMAND" = "$PROMPT_COMMAND" ] || [ -n "$COMP_LINE" ]; then return fi printf "\33[2J" } PROMPT_COMMAND="prompt_on_top" trap 'pre_cmd' DEBUG bash have PROMPT_COMMAND, which hold the command will be executed before bash show prompt. ...


4

You can use shell globbing: for f in *.c; do cp -- "$f" "$OTHERDIR/old#$f"; done The for variable in GLOB format will expand the glob to all matching files/directories (excluding hidden-ones) and iterate over them, saving each in turn as $variable (in the example above, $f). So, the command I show will iterate over all non-hidden files, copying them and ...


3

In bash, there is an option that instructs bash to record a timestamp with each command. You do this by assigning a useful value to the environment variable HISTTIMEFORMAT. From the bash manpage: If this variable is set and not null, its value is used as a format string for strftime(3) to print the time stamp associated with each history entry ...


3

You could only work around that issue with that for example: cat <(false || kill $$) <(echo ok) other_command The subshell of the script is SIGTERMd before the second command can be executed (other_command). The echo ok command is executed "sometimes": The problem is that process substitutions are asynchronous. There's no guarantee that the kill $$ ...


3

program & will still have the same stdin and stdout as program, but program & usually (depending on your terminal settings) won't be able to read from stdin without getting stopped by the system via the SIGTTIN signal (see https://en.wikipedia.org/wiki/Job_control_(Unix)#Implementation ). Background processes won't receive signals from keyboard ...


3

To make a zombie process: $ (sleep 1 & exec /bin/sleep 10) This replace the shell which run sleep 1 with /bin/sleep 10 that won't know the sleep 1 process terminated, so creating a zombie in 10 seconds. I'm not sure what do you want when killing a zombie process. A zombie process was already dead, you can not kill it. Actually, you can make zombie ...


3

exec bash should replace the current shell process with (a new instance of) bash.


3

This is a really weird requirement. Why would you care what interactive shell invoked your script, or invoked some other program that isn't an interactive shell which in turn invoked your script? This has a very strong smell of a XY problem. IF you really need to know, you can try to figure it out, but I don't think there's a fully reliable method, just one ...


3

You can use the rename utility with the appropriate regexes. Assuming the transformation is: nondigits digits underscore nondots dot anything into nondots atsign digits nondigits dot anything the command would be: $ prename -n 's/([^\d]*)(\d*)_([^.]*)\.(.*)$/$3\@$2$1.$4/' * The last * expands it to apply to all files matched by that glob and the -n ...



Only top voted, non community-wiki answers of a minimum length are eligible