1.[ expr ] isused to test whether expr is true or not, 'expr 运算'用来取值
如
if [ $# -eq 2 ]; then
echo "parameter 1 add parameter 2 is `expr $1 + $2`"
fi
如果运行时给参数sh ./wx/test.sh 1 2
输出结果parameter 1 add parameter 2 is 3
2. 各种流程语句的写法
2.1 if 语句
if [ $# -eq 0 ] ; then
exit 1
fi
if [ -f "test.sh" ]
then
echo "exist"
else
echo "not exist"
fi
else里面可以再嵌套if else
2.2 while语句
i=0
while [ $i -lt 3 ]
do
echo $i
i=`expr $i + 1`
done
输出结果
0
1
2
2.3 case语句
if [ -n $1 ];then
car=$1
case $car in
"bentian")
echo "bentian"
echo "show bentian";;
"baoma")
echo "baoma"
echo "show baoma";;
*)
echo "unknown";;
esac
fi
运行时给参数sh ./wx/test.sh bentian
输出结果
bentian
show bentian
给参数sh ./wx/test.sh ben
输出结果
unknown
2.4 for语句