正文
一条 if 语句的结束标记是 fi。可以使用 elif 关键字和多个 if 语句串起来。这个关键字表示“else if”。例如:
if [ $base -eq 1 ] && [ $dm -eq 1 ] ; then installDMBase elif [ $base -ne 1 ] && [ $dm -eq 1 ]; then installBase elif [ $base -eq 1 ] && [ $dm -ne 1]; then installBase else echo '==> Installing nothing' fi
sh 的数值和字符比较运算符。sh 使用文字运算符(text operator)进行数值比较,使用符号运算符(symbolic operator)进行字符比较。
| 字符串 | 数值 | 为真的条件 |
| x=y | x -eq y | x 等于 y |
| x!=y | x -ne y | x 不等于 y |
| x<y | x -lt y | x 小于 y |
| n/a | x -le y | x 小于或等于 y |
| x>y | x -gt y | x 大于 y |
| n/a | x -ge y | x 大于或等于 y |
| -n x | n/a | x 不为空 |
| -z | n/a | x 为空 |
sh 的众多文件测试与文件比较操作符
| 操作符 | 为真的条件 |
| -d file | file 存在且为目录 |
| -e file | file 存在 |
| -f file | file 存在且为普通文件 |
| -r file | 用户对 file 有读权限 |
| -s file | file 存在且不为空 |
| -w file | 用户对 file 有写权限 |
| file1 -nt file2 | file1 比 file2 新 |
| file1 -ot file | file1 比 file2 旧 |