第一种语法格式:
if 条件判断
then
command
fi
eg.
#!/bin/sh
if test 3 -eq 3
then
echo Yes
fi
第二种语法格式:
if 条件判断
then
command
esle
command
fi
eg.
#!/bin/sh
if [ -d /home/user01/demo ]
then
ls /home/user01/demo
else
mkdir /home/user01/demo
echo 目录创建成功!!
fi
第三种语法格式:
if 条件判断
then
command
elif 条件判断
then
command
elif 条件判断
then
command
...
else
command
fi
eg3.
#!/bi/sh
# read socre and choose level
# read 相当于Java中的scanner 是一种交互式命令,读取设备的输入
read score
if test $score -ge 90
then
echo level A
elif [ $score -ge 80 ] && [ $score -lt 90 ]
then echo "level B"
elif [ $score -ge 70 ] && [ $score -lt 80 ]
then echo "level C"
elif [ $score -ge 60 ] && [ $score -lt 70 ]
then echo "level D"
else
echo 不及格
fi
#!/bi/sh
# read socre and choose level
# read 相当于Java中的scanner 是一种交互式命令,读取设备的输入
echo "请输入学生成绩score:"
read score
if test $score -ge 90;
then
echo level A;
elif [ $score -ge 80 -a $score -lt 90 ]
then
echo "level B";
elif test $score -ge 70 -a $score -lt 80
then
echo "level C";
elif [ $score -ge 60 ] && [ $score -lt 70 ]
then
echo "level D";
else
echo 不及格;
fi