How do I redirect output to a variable in shell? [duplicate]
Use the $( … ) construct: hash=$(genhash –use-ssl -s $IP -p 443 –url $URL | grep MD5 | grep -c $MD5)
Use the $( … ) construct: hash=$(genhash –use-ssl -s $IP -p 443 –url $URL | grep MD5 | grep -c $MD5)
Read a file line by line and execute commands: 4+ answers This is because there is not only 1 answer… Shell command line expansion xargs dedicated tool while read with some remarks while read -u using dedicated fd, for interactive processing (sample) running shell with inline generated script Regarding the OP request: running chmod on … Read more
You are using | (pipe) to direct the output of a command into another command. What you are looking for is && operator to execute the next command only if the previous one succeeded: cp /templates/apple /templates/used && cp /templates/apple /templates/inuse && rm /templates/apple Or cp /templates/apple /templates/used && mv /templates/apple /templates/inuse To summarize (non-exhaustively) … Read more
Normally bash doesn’t interpret escape sequences in string literals. So if you write \n or “\n” or ‘\n’, that’s not a linebreak – it’s the letter n (in the first case) or a backslash followed by the letter n (in the other two cases). $’somestring’ is a syntax for string literals with escape sequences. So … Read more
#!/usr/bin/env bash SCRIPT_DIR=$( cd — “$( dirname — “${BASH_SOURCE[0]}” )” &> /dev/null && pwd ) is a useful one-liner which will give you the full directory name of the script no matter where it is being called from. It will work as long as the last component of the path used to find the script … Read more
Bash can’t handle floats. Pipe to bc instead: if [ $(echo ” $test > $k” | bc) -eq 1 ] The error you see though is because the test command (i.e. the [) needs spaces before and after It is even better to use (( … )) since you compare numbers like this: if (( … Read more
Use the date command’s ability to add days to existing dates. The following: DATE=2013-05-25 for i in {0..8} do NEXT_DATE=$(date +%m-%d-%Y -d “$DATE + $i day”) echo “$NEXT_DATE” done produces: 05-25-2013 05-26-2013 05-27-2013 05-28-2013 05-29-2013 05-30-2013 05-31-2013 06-01-2013 06-02-2013 Note, this works well in your case but other date formats such as yyyymmdd may need … Read more
Yes. Use the -n option: echo -n “$x” From help echo: -n do not append a newline This would strips off the last newline too, so if you want you can add a final newline after the loop: for …; do …; done; echo Note: This is not portable among various implementations of echo builtin/external … Read more
bash does not support floating-point arithmetic. You need to use an external utility like bc. # Like everything else in shell, these are strings, not # floating-point values d1=0.003 d2=0.0008 # bc parses its input to perform math d1d2=$(echo “$d1 + $d2” | bc) # These, too, are strings (not integers) mean1=7 mean2=5 # $((…)) … Read more
It redirects the string to stdin of the command. Variables assigned directly before the command in this way only take effect for the command process; the shell remains untouched.