码迷,mamicode.com
首页 > 其他好文 > 详细

case

时间:2016-06-09 01:01:51      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:command   程序   number   

shell中的case判断
格式: case  变量名 in 
                     value1)
                          command
                          ;;
                     value2)
                          command
                          ;;
                      *)
                        commond
                            ;;
                      esac

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

2|3) 
    command
    ;;

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

案例:


  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 ]

  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 "不及格"

  36.         ;;

  37.     2)

  38.         echo "及格"

  39.         ;;

  40.     3|4)

  41.         echo "优秀"

  42.         ;;

  43.     *)

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

  45.         ;;

  46. esac


case

标签:command   程序   number   

原文地址:http://xiongrunchu.blog.51cto.com/11696174/1787511

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