TEST(2) 字符串测试 -nSTRING , STRING # the length of STRING is nonzero -z STRING # the length of STRING is zero STRING1 = STRING2 # the strings are equal STRING1 != STRING2 # the strings are not equal
23.
TEST(3) 数值测试 INTEGER1-eq INTEGER2 # INTEGER1 is equal to INTEGER2 INTEGER1 -ne INTEGER2 # INTEGER1 is not equal to INTEGER2 INTEGER1 -ge INTEGER2 # INTEGER1 is greater than or equal to INTEGER2 INTEGER1 -gt INTEGER2 # INTEGER1 is greater than INTEGER2 INTEGER1 -le INTEGER2 # INTEGER1 is less than or equal to INTEGER2 INTEGER1 -lt INTEGER2 # INTEGER1 is less than INTEGER2
24.
TEST(4) 文件测试 -dFILE # FILE exists and is a directory -e FILE # FILE exists -f FILE # FILE exists and is a regular file -h FILE # FILE exists and is a symbolic link (same as -L) -L FILE # FILE exists and is a symbolic link (same as -h) -r FILE # FILE exists and read permission is granted -w FILE # FILE exists and write permission is granted -x FILE # FILE exists and execute permission is granted -s FILE # FILE exists and has a size greater than zero
25.
TEST(4) 文件测试 FILE1-ef FILE2 # FILE1 and FILE2 have the same device and inode numbers FILE1 -nt FILE2 # FILE1 is newer (modification date) than FILE2 FILE1 -ot FILE2 # FILE1 is older than FILE2
26.
循环结构 (1) forarg in [ list ]; do command(s)... done 注意 : 在循环的每次执行中 ,arg 将顺序的存取 list 中列出的变量 . for arg in "$var1" "$var2" "$var3" ... "$varN" # 在第 1 次循环中 , arg = $var1 # 在第 2 次循环中 , arg = $var2 # 在第 3 次循环中 , arg = $var3 # ... # 在第 n 次循环中 , arg = $varN
27.
循环结构 (2) forarg in `seq (FIRST) (INCREMENT) LAST` for number in `echo $NUMBERS ` 或 for number in $(echo $NUMBERS) # 使用命令替换来产生 for 循环的 [list] ;事先声明 NUMBERS 或 ((a=1; a <= LIMIT ; a++)) # C 风格的 for 循环 for file in $FILES # 事先声明 FILES for file in * # 文件名扩展(当前目录) for arg # 忽略 [list] 的话 , 将会使循环操作 $@( 从命令行传递给脚本的参数列表 )
28.
循环结构 (3) while[ condition ]; do command(s)... done while (( a <= LIMIT )) # while 循环也可通过 (()) 来使用 C 风格语法 while read VAR1 VAR2 ...; do command(s)... done < filename
29.
循环结构 (4) until[condition-is-true]; do command(s)... done break continue
30.
分支结构 (1) if[[ condition1 ]]; then command(s)... elif [[ condition2 ]]; then command(s)... else command(s)... fi (( a <= LIMIT )) commands(s)... # 也可通过 (()) 来使用 C 风格语法, (()) 结构扩展并计算一个算术表达式的结果
更多 鸟哥的 Linux私房菜 . A Practical Guide To Linux Commands Editors And Shell Programming , Mark G. Sobell. Advanced Bash-Scripting Guide , Mendel Cooper. Shell_12P, Shell_13Q. Learning the vi and Vim Editors (7 th ), Arnold Robbins et al. Computer Systems: A Programmer’s Perspective, Randal E. Bryant and David R. O’Hallaron. The C Programming Language, K&R. 用 GDB 调试程序 跟我一起写 makefile APUE (2 nd )/UNP/TCPIP_Illustrated, Richard Stevens.