case基础语法:
格式 case 变量名 in
value1)
command
;;
value2)
command
;;
*)
commond
;;
esac
在case程序中,可以在条件中使用|,表示或的意思, 比如
2|3)
command
;;
脚本
[root@lynn-04 shell]# vim case.sh#!/bin/bash
read -p "Please input a number: " n
if [ -z "$n" ]
thenecho "Please input a number."exit 1
fin1=`echo $n|sed 's/[0-9]//g'`
if [ -n "$n1" ]
thenecho "Please input a number."exit 1
fiif [ $n -lt 60 ] && [ $n -ge 0 ]
thentag=1
elif [ $n -ge 60 ] && [ $n -lt 80 ]
thentag=2
elif [ $n -ge 80 ] && [ $n -lt 90 ]
thentag=3
elif [ $n -ge 90 ] && [ $n -le 100 ]
thentag=4
elsetag=0
fi
case $tag in1)echo "不及格";;2)echo "及格";;3)echo "优秀";;4)echo "非常优秀";;*)echo "The number range is 0-100.";;
esac
执行结果
[root@lynn-04 shell]# sh case.sh
Please input a number: 50
不及格
[root@lynn-04 shell]# sh case.sh
Please input a number: 60
及格
[root@lynn-04 shell]# sh case.sh
Please input a number: 80
优秀
[root@lynn-04 shell]# sh case.sh
Please input a number: 90
非常优秀
[root@lynn-04 shell]# sh case.sh
Please input a number: 116
The number range is 0-100.
[root@lynn-04 shell]# sh case.sh
Please input a number: aaa
Please input a number.
转载于:https://blog.51cto.com/10963213/2105430