on
In A Shell
This is a guide to shell utilities. Mainly based on The book Linux Shell Scripting Cookbook, here is what I took.
-
printfin shell: C-style,%-5smeans left alignment, 5 widths;%-4.2fmeans float, 2 rounding, 4 width. -
colorin shell: reset=0, black=30, red=31, green=32, yellow=33, blue=34, magenta=35, cyan=36, white=37. For background, plus 10 except reset. Use it like thisecho -e "\e[1;31m This is red text \e[0m", e flag means escape the double quote. -
variablesin shell: direct assignment, but referring with$, sometimes${variable};${#variable}gives you length; use[ ]or(( ))if multiple variables trouble you.#remove from beginning%remove from end/remove or replace
e.g. ${VAR%%PATTERN} ${VAR/PATTERN/REPLACE}
Guess what $1 & $2 and more means. $? brings the last return value.
-
redirection & pipein shell: use>, for stdout 1, stderr 2, use1>,2>respectively, convert stderr to stdout can also be accomplished bycmd 2>&1 output.txtorcmd &> output.txt; of course, to get away with these alloying errors, throw them into the “Black Hole”,cmd 2> /dev/null.>,<,>>, directions do not matter if you know what you are doing.teewrites stdin to file -
Arrays and Associative arraysin shell:array = (1 2 3), no comma, indexing is the same as squre bracket indexing, to retrieve all,arr[*]orarr[@].declare -A ass_arrayandass_array=([index1]=val1 [index2]=val2), there you go. But you have to${!ass_array[*]}to get all items. -
datein shell:date +%sUnix time, yeah! You can also offer a date for operation, other specification can be found in help, likedate --date "Jan 20 2001" +%Agives you exactly the weekday. This is limitless. -
functionin shell: Declaration is quite C-style, however, passing args usingfunName arg1 arg2.read -n 2 varreads 2 vars you provide. Other arguments like-s/-d/-P/-tcan also be useful and will be found in detail in help or manual.for do done,if then fi, appends;all the time. -eq/ne/lt/le/gt/ge/ -r/w/x/f/d/e/c/b/ -a/o -
findin shell:find . -regex ".*\(\.py\|\.sh)$", another cursed string problem, escape, escape, escape.-printis handy,-type/-sizeare useful, caution with-delete. -
xargs and morein shell:echo "splitXsplitXsplitXsplit" | xargs -d X -n 2, amazing.wdandtreven more astonishing.paste,cut,sort,mktemp,uniq,split,diff,commblah blah;echo ${URL%.*}or#, sometimes double, can actually save you.tail,headcd -, this is brilliant!
-sed & awk in shell:
If you are familiar with vim, you will definitely feel at home.