【CentOS 7Shell编程4】,shell中的case判断#180208

hellopasswd


shell中的case判断

  • 格式case 变量名in value1) command ;; value2) command ;; *) command ;; easc
  • 在case程序中,可以在条件中使用|,表示或的意思,比如2|3 command ;;
      1 #!/bin/bash
      2 read -p "Please input a number:" n
      3 if [ -z $n ]
      4 then
      5     echo "Please input a number."
      6     exit 1
      7 fi
      8 n1=`echo $n|sed 's/[0-9]//g'`
      9 if [ ! -z $n1 ]
     10 then
     11     echo "Please input a number."
     12     exit 1
     13 #elif [ $n -lt 0] || [ $n -gt 100 ]
     14 #then
     15 #    echo "The number range is 0-100."
     16 #    exit 1
     17 fi
     18 if [ $n -lt 60 ] && [ $n -ge 0 ]
     19 then
     20     tag=1
     21 elif [ $n -ge 60 ] && [$n -lt 80 ]
     22 then
     23     tag=2
     24 elif [ $n -ge 80 ] && [ $n -lt 90 ]
     25 then
     26     tag=3
     27 elif [ $n -ge 90 ] && [ $n -le 100 ]
     28 then 
     29     tag=4
     30 else
     31     tag=0
     32 fi
     33 case $tag in
     34     1)
     35         echo "1"
     36     ;;
     37     2)
     38         echo "2"
     39     ;;
     40     3|4)
     41         echo "3"
     42     ;;
     43     *)
     44         echo "The number range is 0-100."
     45     ;;
     46 esac



[root@localhost shell]# sh -x 1.sh 
+ read -p 'Please input a number:' n
Please input a number:101
+ '[' -z 101 ']'
++ sed 's/[0-9]//g'
++ echo 101
+ n1=
+ '[' '!' -z ']'
+ '[' 101 -lt 60 ']'
+ '[' 101 -ge 60 ']'
+ '[' 101 -lt 80 ']'
+ '[' 101 -ge 80 ']'
+ '[' 101 -lt 90 ']'
+ '[' 101 -ge 90 ']'
+ '[' 101 -le 100 ']'
+ tag=0
+ case $tag in
+ echo 'The number range is 0-100.'
The number range is 0-100.
[root@localhost shell]# sh -x 1.sh 
+ read -p 'Please input a number:' n
Please input a number:59
+ '[' -z 59 ']'
++ echo 59
++ sed 's/[0-9]//g'
+ n1=
+ '[' '!' -z ']'
+ '[' 59 -lt 60 ']'
+ '[' 59 -ge 0 ']'
+ tag=1
+ case $tag in
+ echo 1
1
[root@localhost shell]# sh -x 1.sh 
+ read -p 'Please input a number:' n
Please input a number:79
+ '[' -z 79 ']'
++ echo 79
++ sed 's/[0-9]//g'
+ n1=
+ '[' '!' -z ']'
+ '[' 79 -lt 60 ']'
+ '[' 79 -ge 60 ']'
+ '[' 79 -lt 80 ']'
+ tag=2
+ case $tag in
+ echo 2
2
[root@localhost shell]# sh -x 1.sh 
+ read -p 'Please input a number:' n
Please input a number:89
+ '[' -z 89 ']'
++ echo 89
++ sed 's/[0-9]//g'
+ n1=
+ '[' '!' -z ']'
+ '[' 89 -lt 60 ']'
+ '[' 89 -ge 60 ']'
+ '[' 89 -lt 80 ']'
+ '[' 89 -ge 80 ']'
+ '[' 89 -lt 90 ']'
+ tag=3
+ case $tag in
+ echo 3
3
[root@localhost shell]# sh -x 1.sh 
+ read -p 'Please input a number:' n
Please input a number:a
+ '[' -z a ']'
++ sed 's/[0-9]//g'
++ echo a
+ n1=a
+ '[' '!' -z a ']'
+ echo 'Please input a number.'
Please input a number.
+ exit 1

#!/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 ] && [ $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 "1"
    ;;
    2)
        echo "2"
    ;;
    3|4)
        echo "3"
    ;;
    *)
        echo "The number range is 0-100."
    ;;
esac

这里的if [ ! -z $n1 ]可以用if [ -n "$n1" ]代替


修改于 180208

转载于:https://my.oschina.net/hellopasswd/blog/1619960

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值