您的位置:首页 > 其它

case

2016-06-08 17:26 351 查看
shell中的case判断
格式: case 变量名 in
value1)
command
;;
value2)
command
;;
*)
commond
;;
esac

在case程序中,可以在条件中使用|,表示或的意思, 比如

2|3)
command
;;

当变量为2或者3时,执行该部分命令。

案例:

#!/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 [ ! -z $n1 ]

then

echo "Please input a number."

exit 1

#elif [ $n -lt 0 ] || [ $n -gt 100 ]

#then

# echo "The number range is 0-100."

# exit 1

fi

if [ $n -lt 60 ]

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|4)

echo "优秀"

;;

*)

echo "The number range is 0-100."

;;

esac
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: