Bash
From Attie's Wiki
(Difference between revisions)
m (Created page with '==Command Not Found== <source lang="bash"> function command_not_found_handle { echo "Hello" return 127 } </source>') |
m (→Print Commands) |
||
(27 intermediate revisions by one user not shown) | |||
Line 1: | Line 1: | ||
+ | ==Useful Arguments== | ||
+ | {| | ||
+ | | <code>-e</code> || treat any non-zero return as an error, and quit | ||
+ | |- | ||
+ | | <code>-u</code> || treat any use of an unset variable as an error, and quit | ||
+ | |- | ||
+ | | <code>-v</code> || print ''unexpanded'' commands as they are executed | ||
+ | |- | ||
+ | | <code>-x</code> || print ''expanded'' commands as they are executed | ||
+ | |} | ||
+ | |||
+ | ===example=== | ||
+ | <source lang="bash"> | ||
+ | #!/bin/bash -eu | ||
+ | |||
+ | echo "hi" | ||
+ | false | ||
+ | echo "there" | ||
+ | </source> | ||
+ | |||
+ | ==Built-in Variables== | ||
+ | {| | ||
+ | | <code>$?</code> || The return code of the last process | ||
+ | |- | ||
+ | | <code>$!</code> || The PID of the most recently started process | ||
+ | |- | ||
+ | | <code>$0</code> || Get the executed file path | ||
+ | |- | ||
+ | | <code>$@</code> || Get all of the command line arguments | ||
+ | |} | ||
+ | |||
+ | ==Print Commands== | ||
+ | <source lang="bash"> | ||
+ | # print commands as they are executed | ||
+ | set -v | ||
+ | # print commands as they are executed, but expand the variables before printing | ||
+ | set -x | ||
+ | </source> | ||
+ | |||
+ | ==cd to the script's directory== | ||
+ | <source lang="bash"> | ||
+ | cd "$(dirname $(readlink -f $0))" | ||
+ | </source> | ||
+ | |||
==Command Not Found== | ==Command Not Found== | ||
<source lang="bash"> | <source lang="bash"> | ||
Line 5: | Line 49: | ||
return 127 | return 127 | ||
} | } | ||
+ | </source> | ||
+ | |||
+ | ==Run as Another User== | ||
+ | {| | ||
+ | ! Option !! Effect | ||
+ | |- | ||
+ | | -n || Non-interactive | ||
+ | |- | ||
+ | | -H || Set the ${HOME} directory to that of the target user | ||
+ | |- | ||
+ | | -u || Run as this user | ||
+ | |} | ||
+ | |||
+ | <source lang="bash"> | ||
+ | if [ "$(whoami)" != "$USER" ]; then | ||
+ | sudo -u "$USER" $0 "$@" | ||
+ | exit $? | ||
+ | fi | ||
+ | </source> | ||
+ | |||
+ | ==Heredoc Without Expanding Variables== | ||
+ | <source lang="bash"> | ||
+ | cat - > /etc/profile.d/mymotd.sh <<'EOF' | ||
+ | hostname=`uname -n` | ||
+ | echo -e "Hostname is $hostname" | ||
+ | EOF | ||
+ | </source> | ||
+ | |||
+ | ==Line-by-Line== | ||
+ | Use the IFS variable (Internal Field Separator), or: | ||
+ | <source lang="bash"> | ||
+ | ls -l | while read -r line; do | ||
+ | echo $line | ||
+ | done | ||
+ | </source> | ||
+ | |||
+ | ==Maths== | ||
+ | <source lang="bash"> | ||
+ | a=5 | ||
+ | b=3 | ||
+ | c=$((a+b)) | ||
+ | echo $a $b $c | ||
+ | </source> | ||
+ | |||
+ | ==Function List== | ||
+ | Get a list of Perl functions, with their line number | ||
+ | <source lang="bash"> | ||
+ | cat gitweb.cgi | grep -n ^sub | sed -re 's/([0-9]+): ?sub ([^ {]+).*/\1:\t\2/' | sort -k2 | less | ||
+ | </source> | ||
+ | |||
+ | ==Timing a Long Process== | ||
+ | <source lang="bash"> | ||
+ | #!/bin/bash -e | ||
+ | |||
+ | EPOCH_START=$(date +%s) | ||
+ | # do the lengthy process... | ||
+ | EPOCH_END=$(date +%s) | ||
+ | |||
+ | echo "" | ||
+ | echo "Operation complete!" | ||
+ | echo " Started: $(date -d@${EPOCH_START})" | ||
+ | echo " Ended: $(date -d@${EPOCH_END})" | ||
+ | DATE_CMD="date -u -d@$((EPOCH_END - EPOCH_START))" | ||
+ | echo " Elapsed: $(($(${DATE_CMD} +'%j')-1)) day(s), $(${DATE_CMD} +'%H:%M:%S')" | ||
+ | </source> | ||
+ | |||
+ | ==Status Printout== | ||
+ | <source lang="bash"> | ||
+ | function print_state() { | ||
+ | ( | ||
+ | date "+%d %b %Y %I:%M %P" | ||
+ | if [ -e /sys/class/thermal/thermal_zone0/temp ]; then | ||
+ | cat /sys/class/thermal/thermal_zone0/temp | tr -d '\n' | ||
+ | echo "mC" | ||
+ | fi | ||
+ | uptime | grep -o 'up [0-9]* days' | ||
+ | uptime | grep -o 'load average: .*' | ||
+ | ) | sed -re ':a;N;$!ba;s/\n/ -- /g' | ||
+ | } | ||
+ | </source> | ||
+ | |||
+ | ==Parent's PID== | ||
+ | <source lang="bash"> | ||
+ | function ppid { | ||
+ | ps -Af 2>/dev/null | awk -v mypid=$(echo $$) '{if($2==mypid)print $3}' | ||
+ | } | ||
+ | </source> | ||
+ | |||
+ | ==runSplice== | ||
+ | This will run the given command, ''n'' times, where ''n'' is the number of records in the given files. | ||
+ | Each iteration will run the given command, appending the line from file A followed to the line from file B. | ||
+ | <source lang="bash"> | ||
+ | function runSplice { | ||
+ | IFS=$'\n' | ||
+ | A=( $(cat $1) ); shift | ||
+ | B=( $(cat $1) ); shift | ||
+ | unset IFS | ||
+ | ALEN=${#A[@]} | ||
+ | BLEN=${#B[@]} | ||
+ | if [ ${ALEN} -ne ${BLEN} ]; then | ||
+ | echo "Files don't have the same numer of records..." | ||
+ | return 1 | ||
+ | fi | ||
+ | echo "Running splice on ${ALEN} records" | ||
+ | echo "Command: $@ {A} {B}" | ||
+ | for i in $(seq 0 $((${ALEN} - 1))); do | ||
+ | echo "$@ \"${A[${i}]}\" \"${B[${i}]}\"" | ||
+ | $@ "${A[${i}]}" "${B[${i}]}" | ||
+ | done | ||
+ | } | ||
+ | </source> | ||
+ | |||
+ | Say you have a list of files <code>a b c</code>, and you want to rename them <code>A B C</code>. You would of course have more files to do than 3... | ||
+ | Do the following: | ||
+ | |||
+ | Contents of <code>a.list</code> | ||
+ | <source lang="text"> | ||
+ | a | ||
+ | b | ||
+ | c | ||
+ | </source> | ||
+ | |||
+ | Contents of <code>b.list</code> | ||
+ | <source lang="text"> | ||
+ | A | ||
+ | B | ||
+ | C | ||
+ | </source> | ||
+ | |||
+ | Now execute: | ||
+ | <source lang="bash"> | ||
+ | runSplice a.list b.list mv | ||
+ | </source> | ||
+ | |||
+ | The following commands will be executed for you: | ||
+ | <source lang="bash"> | ||
+ | mv "a" "A" | ||
+ | mv "b" "B" | ||
+ | mv "c" "C" | ||
+ | </source> | ||
+ | |||
+ | ==If conditions== | ||
+ | {| | ||
+ | ! Primary !! Meaning | ||
+ | |- | ||
+ | | <code>[ -a FILE ]</code> || True if FILE exists. | ||
+ | |- | ||
+ | | <code>[ -b FILE ]</code> || True if FILE exists and is a block-special file. | ||
+ | |- | ||
+ | | <code>[ -c FILE ]</code> || True if FILE exists and is a character-special file. | ||
+ | |- | ||
+ | | <code>[ -d FILE ]</code> || True if FILE exists and is a directory. | ||
+ | |- | ||
+ | | <code>[ -e FILE ]</code> || True if FILE exists. | ||
+ | |- | ||
+ | | <code>[ -f FILE ]</code> || True if FILE exists and is a regular file. | ||
+ | |- | ||
+ | | <code>[ -g FILE ]</code> || True if FILE exists and its SGID bit is set. | ||
+ | |- | ||
+ | | <code>[ -h FILE ]</code> || True if FILE exists and is a symbolic link. | ||
+ | |- | ||
+ | | <code>[ -k FILE ]</code> || True if FILE exists and its sticky bit is set. | ||
+ | |- | ||
+ | | <code>[ -p FILE ]</code> || True if FILE exists and is a named pipe (FIFO). | ||
+ | |- | ||
+ | | <code>[ -r FILE ]</code> || True if FILE exists and is readable. | ||
+ | |- | ||
+ | | <code>[ -s FILE ]</code> || True if FILE exists and has a size greater than zero. | ||
+ | |- | ||
+ | | <code>[ -t FD ]</code> || True if file descriptor FD is open and refers to a terminal. | ||
+ | |- | ||
+ | | <code>[ -u FILE ]</code> || True if FILE exists and its SUID (set user ID) bit is set. | ||
+ | |- | ||
+ | | <code>[ -w FILE ]</code> || True if FILE exists and is writable. | ||
+ | |- | ||
+ | | <code>[ -x FILE ]</code> || True if FILE exists and is executable. | ||
+ | |- | ||
+ | | <code>[ -O FILE ]</code> || True if FILE exists and is owned by the effective user ID. | ||
+ | |- | ||
+ | | <code>[ -G FILE ]</code> || True if FILE exists and is owned by the effective group ID. | ||
+ | |- | ||
+ | | <code>[ -L FILE ]</code> || True if FILE exists and is a symbolic link. | ||
+ | |- | ||
+ | | <code>[ -N FILE ]</code> || True if FILE exists and has been modified since it was last read. | ||
+ | |- | ||
+ | | <code>[ -S FILE ]</code> || True if FILE exists and is a socket. | ||
+ | |- | ||
+ | | <code>[ FILE1 -nt FILE2 ]</code> || True if FILE1 has been changed more recently than FILE2, or if FILE1 exists and FILE2 does not. | ||
+ | |- | ||
+ | | <code>[ FILE1 -ot FILE2 ]</code> || True if FILE1 is older than FILE2, or is FILE2 exists and FILE1 does not. | ||
+ | |- | ||
+ | | <code>[ FILE1 -ef FILE2 ]</code> || True if FILE1 and FILE2 refer to the same device and inode numbers. | ||
+ | |- | ||
+ | | <code>[ -o OPTIONNAME ]</code> || True if shell option "OPTIONNAME" is enabled. | ||
+ | |- | ||
+ | | <code>[ -z STRING ]</code> || True of the length if "STRING" is zero. | ||
+ | |- | ||
+ | | <code>[ -n STRING ] or [ STRING ]</code> || True if the length of "STRING" is non-zero. | ||
+ | |- | ||
+ | | <code>[ STRING1 == STRING2 ]</code> || True if the strings are equal. "=" may be used instead of "==" for strict POSIX compliance. | ||
+ | |- | ||
+ | | <code>[ STRING1 != STRING2 ]</code> || True if the strings are not equal. | ||
+ | |- | ||
+ | | <code>[ STRING1 < STRING2 ]</code> || True if "STRING1" sorts before "STRING2" lexicographically in the current locale. | ||
+ | |- | ||
+ | | <code>[ STRING1 > STRING2 ]</code> || True if "STRING1" sorts after "STRING2" lexicographically in the current locale. | ||
+ | |- | ||
+ | | <code>[ ARG1 OP ARG2 ]</code> || "OP" is one of -eq, -ne, -lt, -le, -gt or -ge. These arithmetic binary operators return true if "ARG1" is equal to, not equal to, less than, less than or equal to, greater than, or greater than or equal to "ARG2", respectively. "ARG1" and "ARG2" are integers. | ||
+ | |} | ||
+ | |||
+ | ==Parse JSON== | ||
+ | {{note|This script may require the following argument added to the awk instances: <code>-F ''</code>}} | ||
+ | <source lang="bash"> | ||
+ | #!/bin/bash | ||
+ | |||
+ | cat json_file | \ | ||
+ | # extract the top-most objects from the JSON | ||
+ | fold -1 | \ | ||
+ | awk 'BEGIN{ depth=0; } | ||
+ | { if ($1 == "{") depth++; | ||
+ | else if ($1 == "}") { | ||
+ | depth--; | ||
+ | if (depth == 0) printf "}\n"; | ||
+ | } | ||
+ | if (depth > 0) printf $1; | ||
+ | } | ||
+ | END{ printf "\n" }' | \ | ||
+ | |||
+ | # strip of the curly braces from each line | ||
+ | sed -re 's/^\{//' -e 's/\}$//' | \ | ||
+ | |||
+ | # process line-by-line | ||
+ | while read -r line; do | ||
+ | |||
+ | # skip blank lines | ||
+ | if [ "${line}" == "" ]; then | ||
+ | continue | ||
+ | fi | ||
+ | |||
+ | echo "JSON Line: ${line}" | ||
+ | |||
+ | # get a list of values/elements (take into account the posibility of a comma _inside_ a string | ||
+ | items=$(echo "${line}" | \ | ||
+ | fold -1 | \ | ||
+ | awk 'BEGIN{ t = 0; q = 0; e = 0; } | ||
+ | { if (q > 0) { | ||
+ | if (!e && $1 == "\"") q--; | ||
+ | } else if (t > 0) { | ||
+ | if (!e && $1 == "'\''") t--; | ||
+ | } else if (!e && $1 == "\"") { | ||
+ | q++; | ||
+ | } else if (!e && $1 == "'\''") { | ||
+ | t++; | ||
+ | } | ||
+ | if (!q && !t && $1 == ",") { | ||
+ | printf "\n"; | ||
+ | } else { | ||
+ | printf $1; | ||
+ | } | ||
+ | if ($1 == "\\") e = 1; | ||
+ | else e = 0; | ||
+ | } | ||
+ | END{ printf "\n" }') | ||
+ | |||
+ | echo "${items}" | while read -r item; do | ||
+ | |||
+ | echo " JSON Item: ${item}" | ||
+ | value=$(echo "${item}" | \ | ||
+ | fold -1 | \ | ||
+ | awk 'BEGIN{ q=0; | ||
+ | e=0; | ||
+ | f=0; | ||
+ | } | ||
+ | { if (f) { | ||
+ | printf $1; | ||
+ | } else if (!e) { | ||
+ | if ($1 == "\"") { | ||
+ | if (!q) { | ||
+ | q = 1; | ||
+ | } else { | ||
+ | q = 0; | ||
+ | } | ||
+ | } else if ($1 == ":") { | ||
+ | f = 1; | ||
+ | } | ||
+ | } | ||
+ | if ($1 == "\\") { | ||
+ | e = 1; | ||
+ | } else { | ||
+ | e = 0; | ||
+ | } | ||
+ | }' | \ | ||
+ | sed -re 's/^"//' -e 's/"$//') | ||
+ | echo " Value: ${value}" | ||
+ | |||
+ | done | ||
+ | done | ||
</source> | </source> |
Latest revision as of 12:06, 27 August 2015
[edit] Useful Arguments
-e |
treat any non-zero return as an error, and quit |
-u |
treat any use of an unset variable as an error, and quit |
-v |
print unexpanded commands as they are executed |
-x |
print expanded commands as they are executed |
[edit] example
#!/bin/bash -eu echo "hi" false echo "there"
[edit] Built-in Variables
$? |
The return code of the last process |
$! |
The PID of the most recently started process |
$0 |
Get the executed file path |
$@ |
Get all of the command line arguments |
[edit] Print Commands
# print commands as they are executed set -v # print commands as they are executed, but expand the variables before printing set -x
[edit] cd to the script's directory
cd "$(dirname $(readlink -f $0))"
[edit] Command Not Found
function command_not_found_handle { echo "Hello" return 127 }
[edit] Run as Another User
Option | Effect |
---|---|
-n | Non-interactive |
-H | Set the ${HOME} directory to that of the target user |
-u | Run as this user |
if [ "$(whoami)" != "$USER" ]; then sudo -u "$USER" $0 "$@" exit $? fi
[edit] Heredoc Without Expanding Variables
cat - > /etc/profile.d/mymotd.sh <<'EOF' hostname=`uname -n` echo -e "Hostname is $hostname" EOF
[edit] Line-by-Line
Use the IFS variable (Internal Field Separator), or:
ls -l | while read -r line; do echo $line done
[edit] Maths
a=5 b=3 c=$((a+b)) echo $a $b $c
[edit] Function List
Get a list of Perl functions, with their line number
cat gitweb.cgi | grep -n ^sub | sed -re 's/([0-9]+): ?sub ([^ {]+).*/\1:\t\2/' | sort -k2 | less
[edit] Timing a Long Process
#!/bin/bash -e EPOCH_START=$(date +%s) # do the lengthy process... EPOCH_END=$(date +%s) echo "" echo "Operation complete!" echo " Started: $(date -d@${EPOCH_START})" echo " Ended: $(date -d@${EPOCH_END})" DATE_CMD="date -u -d@$((EPOCH_END - EPOCH_START))" echo " Elapsed: $(($(${DATE_CMD} +'%j')-1)) day(s), $(${DATE_CMD} +'%H:%M:%S')"
[edit] Status Printout
function print_state() { ( date "+%d %b %Y %I:%M %P" if [ -e /sys/class/thermal/thermal_zone0/temp ]; then cat /sys/class/thermal/thermal_zone0/temp | tr -d '\n' echo "mC" fi uptime | grep -o 'up [0-9]* days' uptime | grep -o 'load average: .*' ) | sed -re ':a;N;$!ba;s/\n/ -- /g' }
[edit] Parent's PID
function ppid { ps -Af 2>/dev/null | awk -v mypid=$(echo $$) '{if($2==mypid)print $3}' }
[edit] runSplice
This will run the given command, n times, where n is the number of records in the given files. Each iteration will run the given command, appending the line from file A followed to the line from file B.
function runSplice { IFS=$'\n' A=( $(cat $1) ); shift B=( $(cat $1) ); shift unset IFS ALEN=${#A[@]} BLEN=${#B[@]} if [ ${ALEN} -ne ${BLEN} ]; then echo "Files don't have the same numer of records..." return 1 fi echo "Running splice on ${ALEN} records" echo "Command: $@ {A} {B}" for i in $(seq 0 $((${ALEN} - 1))); do echo "$@ \"${A[${i}]}\" \"${B[${i}]}\"" $@ "${A[${i}]}" "${B[${i}]}" done }
Say you have a list of files a b c
, and you want to rename them A B C
. You would of course have more files to do than 3...
Do the following:
Contents of a.list
a b c
Contents of b.list
A B C
Now execute:
runSplice a.list b.list mv
The following commands will be executed for you:
mv "a" "A" mv "b" "B" mv "c" "C"
[edit] If conditions
Primary | Meaning |
---|---|
[ -a FILE ] |
True if FILE exists. |
[ -b FILE ] |
True if FILE exists and is a block-special file. |
[ -c FILE ] |
True if FILE exists and is a character-special file. |
[ -d FILE ] |
True if FILE exists and is a directory. |
[ -e FILE ] |
True if FILE exists. |
[ -f FILE ] |
True if FILE exists and is a regular file. |
[ -g FILE ] |
True if FILE exists and its SGID bit is set. |
[ -h FILE ] |
True if FILE exists and is a symbolic link. |
[ -k FILE ] |
True if FILE exists and its sticky bit is set. |
[ -p FILE ] |
True if FILE exists and is a named pipe (FIFO). |
[ -r FILE ] |
True if FILE exists and is readable. |
[ -s FILE ] |
True if FILE exists and has a size greater than zero. |
[ -t FD ] |
True if file descriptor FD is open and refers to a terminal. |
[ -u FILE ] |
True if FILE exists and its SUID (set user ID) bit is set. |
[ -w FILE ] |
True if FILE exists and is writable. |
[ -x FILE ] |
True if FILE exists and is executable. |
[ -O FILE ] |
True if FILE exists and is owned by the effective user ID. |
[ -G FILE ] |
True if FILE exists and is owned by the effective group ID. |
[ -L FILE ] |
True if FILE exists and is a symbolic link. |
[ -N FILE ] |
True if FILE exists and has been modified since it was last read. |
[ -S FILE ] |
True if FILE exists and is a socket. |
[ FILE1 -nt FILE2 ] |
True if FILE1 has been changed more recently than FILE2, or if FILE1 exists and FILE2 does not. |
[ FILE1 -ot FILE2 ] |
True if FILE1 is older than FILE2, or is FILE2 exists and FILE1 does not. |
[ FILE1 -ef FILE2 ] |
True if FILE1 and FILE2 refer to the same device and inode numbers. |
[ -o OPTIONNAME ] |
True if shell option "OPTIONNAME" is enabled. |
[ -z STRING ] |
True of the length if "STRING" is zero. |
[ -n STRING ] or [ STRING ] |
True if the length of "STRING" is non-zero. |
[ STRING1 == STRING2 ] |
True if the strings are equal. "=" may be used instead of "==" for strict POSIX compliance. |
[ STRING1 != STRING2 ] |
True if the strings are not equal. |
[ STRING1 < STRING2 ] |
True if "STRING1" sorts before "STRING2" lexicographically in the current locale. |
[ STRING1 > STRING2 ] |
True if "STRING1" sorts after "STRING2" lexicographically in the current locale. |
[ ARG1 OP ARG2 ] |
"OP" is one of -eq, -ne, -lt, -le, -gt or -ge. These arithmetic binary operators return true if "ARG1" is equal to, not equal to, less than, less than or equal to, greater than, or greater than or equal to "ARG2", respectively. "ARG1" and "ARG2" are integers. |
[edit] Parse JSON
Note: This script may require the following argument added to the awk instances:
-F ''
#!/bin/bash cat json_file | \ # extract the top-most objects from the JSON fold -1 | \ awk 'BEGIN{ depth=0; } { if ($1 == "{") depth++; else if ($1 == "}") { depth--; if (depth == 0) printf "}\n"; } if (depth > 0) printf $1; } END{ printf "\n" }' | \ # strip of the curly braces from each line sed -re 's/^\{//' -e 's/\}$//' | \ # process line-by-line while read -r line; do # skip blank lines if [ "${line}" == "" ]; then continue fi echo "JSON Line: ${line}" # get a list of values/elements (take into account the posibility of a comma _inside_ a string items=$(echo "${line}" | \ fold -1 | \ awk 'BEGIN{ t = 0; q = 0; e = 0; } { if (q > 0) { if (!e && $1 == "\"") q--; } else if (t > 0) { if (!e && $1 == "'\''") t--; } else if (!e && $1 == "\"") { q++; } else if (!e && $1 == "'\''") { t++; } if (!q && !t && $1 == ",") { printf "\n"; } else { printf $1; } if ($1 == "\\") e = 1; else e = 0; } END{ printf "\n" }') echo "${items}" | while read -r item; do echo " JSON Item: ${item}" value=$(echo "${item}" | \ fold -1 | \ awk 'BEGIN{ q=0; e=0; f=0; } { if (f) { printf $1; } else if (!e) { if ($1 == "\"") { if (!q) { q = 1; } else { q = 0; } } else if ($1 == ":") { f = 1; } } if ($1 == "\\") { e = 1; } else { e = 0; } }' | \ sed -re 's/^"//' -e 's/"$//') echo " Value: ${value}" done done