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.
-
printf
in shell: C-style,%-5s
means left alignment, 5 widths;%-4.2f
means float, 2 rounding, 4 width. -
color
in 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. -
variables
in 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 & pipe
in shell: use>
, for stdout 1, stderr 2, use1>
,2>
respectively, convert stderr to stdout can also be accomplished bycmd 2>&1 output.txt
orcmd &> 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.tee
writes stdin to file -
Arrays and Associative arrays
in shell:array = (1 2 3)
, no comma, indexing is the same as squre bracket indexing, to retrieve all,arr[*]
orarr[@]
.declare -A ass_array
andass_array=([index1]=val1 [index2]=val2)
, there you go. But you have to${!ass_array[*]}
to get all items. -
date
in shell:date +%s
Unix time, yeah! You can also offer a date for operation, other specification can be found in help, likedate --date "Jan 20 2001" +%A
gives you exactly the weekday. This is limitless. -
function
in shell: Declaration is quite C-style, however, passing args usingfunName arg1 arg2
.read -n 2 var
reads 2 vars you provide. Other arguments like-s/-d/-P/-t
can 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 -
find
in shell:find . -regex ".*\(\.py\|\.sh)$"
, another cursed string problem, escape, escape, escape.-print
is handy,-type/-size
are useful, caution with-delete
. -
xargs and more
in shell:echo "splitXsplitXsplitXsplit" | xargs -d X -n 2
, amazing.wd
andtr
even more astonishing.paste
,cut
,sort
,mktemp
,uniq
,split
,diff
,comm
blah blah;echo ${URL%.*}
or#
, sometimes double, can actually save you.tail
,head
cd -
, this is brilliant!
-sed & awk
in shell:
If you are familiar with vim, you will definitely feel at home.