Refs:
1. Linux Shell Scripting Cookbook(2nd Edition)
Table of Contents:
Preface
Chapter 1: Shell Something Out
Chapter 2: Have a Good Command
Chapter 3: File In, File Out
Chapter 4: Texting and Driving
Chapter 5: Tangled Web? Not At All!
Chapter 6: The Backup Plan
Chapter 7: The Old-boy Network
Chapter 8: Put on the Monitor‘s Cap
Chapter 9: Administration Calls
Index
说明:本文为书摘,例子多为在原书基础上的修改,为了让篇幅不太庞大,例子尽量缩为一行,使书摘结构更清晰
正文:
Preface$
Chapter 1: Shell Something Out
1. Bash (Bourne Again Shell); #(root) and $(non root); #!/bin/bash (Shebang), `bash script.sh`
2. ";" is like a newline in bash
3. Printing in the terminal (`echo` & `printf`)
1. echo: "some $text"(some ***),(!!! thing) , ‘some $text‘(some $text)
2. echo: "-n" no newline; "-e" convert "\t\n" etc
3. `printf "%-10s %4.2f\n" price 4.24; "-" for [left align]
4. Colored output:
Text: reset = 0, black = 30, red = 31, green = 32, yellow = 33, blue = 34, magenta = 35, cyan = 36, and white = 37
Back: reset = 0, black = 40, red = 41, green = 42, yellow = 43, blue = 44, magenta = 45, cyan = 46, and white=47
Usage: echo -e "\e[1;42m \e[1;31m Green Background, red Text \e[0m"
4. Variables and Environment Variables
1. cat /proc/$PID/environ | tr ‘\0‘ ‘\n‘ (‘\0‘ null character); `echo $PATH | tr [=:=] ‘\n‘`
2. to get $PID: pgrep geany ===> 1420
3. length=${#var}
4. `if [ $UID -ne 0 ]; then echo "you are not root"; else echo "you are root"; fi`
5. Modify the Bash Prompt String
1. cat ~/.bashrc | grep "PS1"
2. \u ==> expands to username
3. \w ==> expands to the current working directory
4. \h ==> expands hostname
6. Math with the shell
1. let
let sum=a+b && echo $sum (a=4, b=6) ==> 10
2. [ math expr ]
sum=$[ a+ b ]; sum=$[ a + 10 ]
3. (( math expr ))
sum=$(( a + b))
4. expr
sum=`expr 24 + 40`
sum=`expr 42 + $a`
5. bc (decimal places, base conversion...)
echo "420 * 04 / 42" | bc
echo "scale=2; 3/8" | bc
7. File descriptors and Redirection
1. [0: stdin], [1: stdout], [2: stderr]
2. ">" redirection, ">>" redirection and append (stdout)
3. "2>", "2>>" (stderr)
4. "&>", "2>&1" (stdout & stderr)
5. Trash Can(Black Hole): /dev/null
6. cat file.txt | tee out.txt | cat -n
7. echo "hello" | tee - - - ("-" stdin), so we got 4 "hello"s
8. Read a file as stdin
cat <<DONE > output.txt
9. Customize file descriptor
1. exec (read mode, write with truncate mode, write with append mode)
2. e.g.
exec 4>>log.txt; echo "hello" >&4; echo "world" >&4; cat log.txt
3. cat<log.txt ("cat<log.txt" or"cat < log.txt", both fine)
8. Arrays and Associative Arrays
1. Arrays
1. array_var=(1 2 3 4 5)
2. echo $array_var <==> echo ${array_var[0]} (echo $array_var[0] works wrongly!!!)
3. echo ${array_var[*]}, echo ${array_var[@]}
4. echo ${array_var[$index]}
5. echo ${#array_var[*]}, echo ${#array_var[@]}
2. Associate Arrays
1. declare -A ass_array
2. ass_array=([gnat]=124 [tang]="name") && echo ${ass_array[tang]} && echo ${ass_array[*]}
3. echo ${!ass_array[*]} ==> output all indexes
9. Visiting Alias
e.g. alias install="sudo apt-get install"
10. Grabbing info about the terminal
1. tput: initialize a terminal or query terminfo database
1. tput cols/lines/longname
2. tput cup 100 100 # set cursor position
3. tput setb/setf NUM # set background/font color, NUM: 0~7 (I think it‘s better than "/e[1;32")
e.g. tput setb 2 setf 7 && echo "green background && white text" # you should "setb" first then "setf"
4. tput bold; tput smul; tput rmul # bold, set underline, remove underline
5. tput ed # I dont understand this...
2. stty: change and print terminal line settings
1. stty -echo # Fantastic way to joke others, 整人的好方法!!
2. stty echo # resume
11. Getting and Setting dates and delays
[你大爷的Fork Bomb!!! 不多抱怨,继续]
1. In Unix-like System: 1970-01-01 00:00:00 UTC( Epoch or Unix Time)
2. date; date +%s; date "+%s"; date --date "Sun Feb 2 18:48:51 CST 2014" +%s (specify date/time)
3. date "+%d %B %Y"
4. Set date/time: date -s "2 Feb 2014 18:48:51" (i prefer not try it)
6. Count time: start=$(date +%s); end=$(date +%s); diff=$((end - start)); echo "passed $diff seconds"
5. Formats:
Weekday: %a(Sun) or %A(Sunday)
Month: %b(Feb) or %B(February)
Day: %d(02)
Date in Format(mm/dd/yy): %D(02/02/14)
Year: %y(14) or %Y(2014)
Hour: %I(06) %H(18), I prefer 24 hour than 12 hour
Minute: %M
Second: %S
Nano Second: %N
Epoch Unix Time in seconds: %s
6. A good Example:
1 #!/bin/bash 2 #Filename: sleep.sh 3 echo -n Count: 4 tput sc 5 count=0; 6 while true; 7 do 8 if [ $count -lt 40 ]; 9 then 10 let count++; 11 sleep 1; 12 tput rc 13 tput ed 14 echo -n $count; 15 else exit 0; 16 fi 17 done
12. Debugging the Script
1. bash -x script.sh
2. Shebang hack: #!/bin/bash -xv # enable debugging
13. Functions and Arguments
1. fname() { echo -n Arg1: $1 " "; echo Arg2: $2; echo Args: $@; return 0;} && fname how are you
2. $@ v.s. $*, we prefer the former, $*(expands as "$1c$2c$3", where c is the first character of IFS), 百度到的IFS说明,by the way, 转载应注明地址
3. The Recusive Fucs:
1. F() { echo $1; sleep 1; F hello;} && F hello # better than the one on the book
2. Fork Bomb(不要在自己电脑上试试!!!完全没看懂,在自己电脑上试试看,结果回到了*!即上文的“你大爷。。”处)
ForkBomb: `:(){ :|:& };:` # 我是没看懂,如果尝试,请先保存在编辑文档。。
We can write a recursive function, which is basically a function that calls itself, It infinitely spawns processes and ends up in a denial-of-service attack.
4. Exporting Functions: export -f fname
5. Reading the return status of last command: cmd; echo $? (exit status)
6. Passing arguments to commands
14. Reading the output of a sequence of commands in a variable
1. Filters: cmd1 | cmd2 | cmd3 e.g. `ls | grep "os" | cat -n > output.txt`
2. Subshell Method: e.g. `cmd_output=$(ls | cat -n)
3. Back tick/ Back quotes: e.g. cmd_output=`ls | cat -n` # I use this a lot, as you can see in this paragraph
4. Subshell bonus:
1. Spawning a separate process with subshell: e.g. `pwd; (cd /usr; ls); pwd`
2. Subshell quoting to preserve spacing and the newline character: out=$(cat file.txt)
15. Read
1. read -n num_of_chars variable: `read -n 4 year`\
2. read -s password # nonecho mode
3. read -p "enter your username: " username # read with a message/prompt
4. read -t seconds var # read with a timeout
5. read -d delim_char var
16. Repeat func:
1. repeat() { while true; do $@ && return; done;}; repeat what_you_want_to_do
2. revised edition: repeat() { while :; do $@ && return; done;}; repeat what_you_want_to_do
3. repeat() { while :; do $@ && return; sleep 30; done;};
4. e.g. repeat wget -c www.sourceforge.com/somefile.tar.gz
17. File Separators and Iterators
1. IFS => Internal Field Iterator
2. CSV => Comma Separated Values
3. e.g. `data="how,are,you"; oldIFS=$IFS; IFS=,; for item in $data; do echo item: $item; done; IFS=$oldIFS
4. echo {a..z}; echo {1..420};
5. for((i=0; i<24; i++)){ echo $i;}
6. while condition; do something; done;
7. until condition; do somthing; done;
18. Comparisons and tests
1. if condition; then do_something; else if condition; then do_something; else do_something; fi;
2. [ condition ] && something_else; [ condition ] || something
1. -gt, -lt, -ge, -le # greater/less than/equal
2. -a, -o # and/or
3. -f, -d # file/dir
4. -x, -e # executable/exists
5. -c, -b # char/block devices
6. -w, -r # writeable/readable
7. -L # symlink
3. e.g.
`if [ -e /bin/bash ]; then echo exists; else echo not exists; fi`
`if [ -e "/bin/bash" ]; then echo exists; else echo not exists; fi` ==> these two are the same
4. String comparason
1. [[ $str1 == $str2 ]], [[ $str1 = $str2 ]] # same
2. [[ $str1 != $str2 ]]
3. [[ $str1 < $str2 ]]; [[ $str1 >$str2 ]]
4. [[ -z $str ]]; [[ -n $str ]] # empty/non-empty
19. My Notice(Gnat‘s notice)
1. echo something } #bad ==> echo soemthing; }
2. do;# bad ==> do :;
[ to be continued ]