case

有条件地执行命令,case将有选择地执行与匹配单词的第一个模式相对应的命令列表

语法

  case word in [ [(] pattern [| pattern]...) command-list ;;]... esac

|' 用于分隔多个模式,以及)’ 运算符终止模式列表,模式列表和关联的命令列表称为子句,直到`;;结束’.

实例1

#!/bin/bash #
a=20 #定义变量值
case $a in         #若变量在下面的某值中,则执行它下面的命令
    10)         #值内容 可以加上 | ,可以有多个值
{
    echo "the a is 10"         #执行输出命令
    echo "over"         #执行输出命令
}
;;         #注意这是两个 ;;
20 |30)         #值内容 可以加上 | ,可以有多个值
{
   echo "the a is 20" #执行输出命令
   echo "over" #执行输出命令
}
;;
*)         #默认匹配内容,如以上都不匹配,则执行这条
echo "hello" #执行输出内容
;;
esac         #结束
echo "test case end"         #输出内容

实例2

 #!/bin/bash #
 echo -n "输入一个动物的名字: "
 read ANIMAL
 echo -n "The $ANIMAL has "
 case $ANIMAL in
    horse | dog | cat) echo -n "four";;
    man | kangaroo ) echo -n "two";;
   *) echo -n "an unknown number of";;
 esac
 echo " legs."

case 命令实例:

linux命令:case $# in 0) echo usage: $0 pattern ;; *)case $1 in */*)sed ' s,'"$1"',\ ,g';; *) sed ' s/'"$1"'/\ /g' ;;esac;esac; ;;esac;esac; /g' s/'"$1"'/\ ' sed *) ,g';; s,'"$1"',\ ' */*)sed in $1 *)case ;; pattern $0 usage: echo 0) in $# case case $# in 0) echo usage: $0 pattern ;; *)case $1 in */*)sed ' s,'"$1"',\ ,g';; *) sed ' s/'"$1"'/\ /g' ;;esac;esac;rootopen.com
linux命令:case $# in 0) echo usage: $0 pattern ;; *)case $1 in */*)sed ' s,'"$1"',\ ,g';; *) sed ' s/'"$1"'/\ /g' ;;esac;esac; ;;esac;esac; /g' s/'"$1"'/\ ' sed *) ,g';; s,'"$1"',\ ' */*)sed in $1 *)case ;; pattern $0 usage: echo 0) in $# case case $# in 0) echo usage: $0 pattern ;; *)case $1 in */*)sed ' s,'"$1"',\ ,g';; *) sed ' s/'"$1"'/\ /g' ;;esac;esac;rootopen.com
2018-01-25 09:54:48

linux命令:case `uname` in FreeBSD)a=$#; case $a in 2) case $1 in 0) jot $(($2+1)) 0 $2 ;; *) jot $2 $1 $2 ;;esac;esac;esac; # usage: seq 1 4; seq 0 4 4 0 seq 4; 1 seq usage: # ;;esac;esac;esac; $2 $1 $2 jot *) ;; $2 0 $(($2+1)) jot 0) in $1 case 2) in $a case FreeBSD)a=$#; in `uname` case case `uname` in FreeBSD)a=$#; case $a in 2) case $1 in 0) jot $(($2+1)) 0 $2 ;; *) jot $2 $1 $2 ;;esac;esac;esac; # usage: seq 1 4; seq 0 4rootopen.com
linux命令:case `uname` in FreeBSD)a=$#; case $a in 2) case $1 in 0) jot $(($2+1)) 0 $2 ;; *) jot $2 $1 $2 ;;esac;esac;esac; # usage: seq 1 4; seq 0 4 4 0 seq 4; 1 seq usage: # ;;esac;esac;esac; $2 $1 $2 jot *) ;; $2 0 $(($2+1)) jot 0) in $1 case 2) in $a case FreeBSD)a=$#; in `uname` case case `uname` in FreeBSD)a=$#; case $a in 2) case $1 in 0) jot $(($2+1)) 0 $2 ;; *) jot $2 $1 $2 ;;esac;esac;esac; # usage: seq 1 4; seq 0 4rootopen.com
2018-02-12 02:02:48

Linux的Bash命令行(A-Z排序)