为了控制脚本的流程,外壳有 while、if、for 和 case 等构造语句。
if 命令用于检查表达式。例如,以下代码测试当前用户是否是 Tux:
if test $USER = "tux"; then echo "Hello Tux." else echo "You are not Tux." fi
测试表达式既可以复杂也可以简单。以下表达式检查文件 foo.txt 是否存在:
if test -e /tmp/foo.txt ; then echo "Found foo.txt" fi
测试表达式也可以缩写在角括号中:
if [ -e /tmp/foo.txt ] ; then echo "Found foo.txt" fi
在 http://www.cyberciti.biz/nixcraft/linux/docs/uniqlinuxfeatures/lsst/ch03sec02.html 上可以找到更多有用表达式。