if-else语句
#ifelse2.sh #判断两个参数的大小 #!/bin/bash if [ $1 -ge $2 ] then echo "$1>=$2" else echo "$1<$2" fi
测试:
多重if-else语句
shell中的else if 语句,简写成 elif
#!/bin/bash #读入一个分数判断等级 read -p "输入一个分数(0,100):" GREAD if [ $GREAD -gt 100 ]; then echo "分数输入错误:$GREAD" elif [ $GREAD -lt 0 ]; then echo "分数输入错误:$GREAD" elif [ $GREAD -ge 90 ]; then echo "$GREAD 分是优秀" elif [ $GREAD -ge 80 ]; then echo "$GREAD 分是良好" elif [ $GREAD -ge 70 ]; then echo "$GREAD 分是中等" elif [ $GREAD -ge 60 ]; then echo "$GREAD 分是及格" else echo "$GREAD 分不及格" fi测试:
case 语句:
#!/bin/bash read time if [ $time -lt 24 ]&&[ $time -gt 0 ]; then case $time in [1-9]|0[1-9]|1[01]) echo "上午好" ;; 1[2-7]) echo "下午好" ;; *) echo "晚上好" esac else echo "输入时间格式错误" fi测试:
lan@lan_ubuntu:~/桌面/program/课本$ ./case.sh -1 输入时间格式错误 lan@lan_ubuntu:~/桌面/program/课本$ ./case.sh 24 输入时间格式错误 lan@lan_ubuntu:~/桌面/program/课本$ ./case.sh 2 上午好 lan@lan_ubuntu:~/桌面/program/课本$ ./case.sh 3 上午好 lan@lan_ubuntu:~/桌面/program/课本$ ./case.sh 11 上午好