case基础语法:
格式 case??变量名 in
value1)
command
;;
value2)
command
;;
*)
commond
;;
esac
在case程序中,可以在条件中使用|,表示或的意思, 比如? ??
2|3)
command
;;
脚本
[[email protected] shell]# vim case.sh
#!/bin/bash
read -p "Please input a number: " n
if [ -z "$n" ]
then
echo "Please input a number."
exit 1
fi
n1=`echo $n|sed ‘s/[0-9]//g‘`
if [ -n "$n1" ]
then
echo "Please input a number."
exit 1
fi
if [ $n -lt 60 ] && [ $n -ge 0 ]
then
tag=1
elif [ $n -ge 60 ] && [ $n -lt 80 ]
then
tag=2
elif [ $n -ge 80 ] && [ $n -lt 90 ]
then
tag=3
elif [ $n -ge 90 ] && [ $n -le 100 ]
then
tag=4
else
tag=0
fi
case $tag in
1)
echo "不及格"
;;
2)
echo "及格"
;;
3)
echo "优秀"
;;
4)
echo "非常优秀"
;;
*)
echo "The number range is 0-100."
;;
esac
执行结果
[[email protected] shell]# sh case.sh
Please input a number: 50
不及格
[[email protected] shell]# sh case.sh
Please input a number: 60
及格
[[email protected] shell]# sh case.sh
Please input a number: 80
优秀
[[email protected] shell]# sh case.sh
Please input a number: 90
非常优秀
[[email protected] shell]# sh case.sh
Please input a number: 116
The number range is 0-100.
[[email protected] shell]# sh case.sh
Please input a number: aaa
Please input a number.
原文地址:http://blog.51cto.com/10963213/2105430
时间: 2024-11-05 21:47:24