在shell 脚本中,除了用if来判断逻辑外,还有一种常用的方式,那就是case了。具体格式为:

case 变量in

value1)

command

;;

value2)

command

;;

value3)

command

;;

*)

command

;;

esac

举例:

[root@bogon ~]# cat case.sh

#/bin/bash

read -p "input a number:" n

a=$[$n%2]

case $a in

1)

  echo "the unm is odd"

  ;;

0)

   echo "the unm is even"

  ;;

*) 

  echo "it is not a num"

  ;;

esac