码迷,mamicode.com
首页 > 系统相关 > 详细

shell编程--case判断

时间:2018-04-19 16:59:19      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:case   shell   if   

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" ]
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

执行结果

[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.

shell编程--case判断

标签:case   shell   if   

原文地址:http://blog.51cto.com/10963213/2105430

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!