1.条件语句
#!/bin/bash
if [ $# -ne 1 ]
then
echo "usage:$0 filename"
exit
fi
if ! [ -e $1 ]
then
echo "$1 not exist"
exit
fi
if [ -d $1 ]
then
echo "$1 is a directory!"
exit
else
if [ -f $1 ]
then
echo "$1 is a common file"
else
echo "unknow"
fi
fi
格式为if…then…else…fi。这里的[ -f $1 ]跟前面的判断语句 test -f $1起一样的作用,注意在左括号后和右括号前要加上空格。shell脚本中的else if叫做elif。
2.多路分支语句case
#!/bin/bash
echo -n "please input score?"
read r
G=$(expr $r / 10)
case $G in
9 | 10) |分割代表多个选项
echo "A"
;; 分号相当于break
8 | 7)
echo "B"
;;
*)
echo "C"
#;; 最后一个分号可以省略
esac