case语句

匿名 (未验证) 提交于 2019-12-02 23:45:01

一case条件语句

case条件语句相当于多分支的if/elif/else条件语句,但是它看起来更规范更工整,常被应用于实现系统服务启动脚本等企业应用场景中。
在case语句中,程序会将case获取的变量的值与表达式部分的值1、值2、值3等逐个进行比较。
如果获取的变量值和某个值(例如值1)相匹配,就会执行值(例如值1)后面对应的指令(例如指令1,其可能是一组指令),直到执行到双分号(;;)才停止。
然后再跳出case语句主体,执行case语句(即esac字符)后面的其他命令。
如果没有找到匹配变量的任何值,则执行"*)"后面的指令(通常是给使用者的使用提示),直到遇到双分号(;;) (此处的双分号可以省略)或esac结束。
这部分相当于if多分支语句中最后的else语句部分。
另外, case语句中表达式对应值的部分,还可以使用管道等更多功能来匹配。

(一)case语句的语法

适合判断变量值



PAT1/ֵ1)
分支1/指令1
;;
PAT2/ֵ2)
分支2/指令2
;;
...
*)
默认分支
;;
esac

case条件语句的执行流程逻辑图:

说明:当变量的值等于值1时,执行指令1;等于值2时执行指令2,以此类推;如果都不符合,则执"*)"后面的指令。
此外,注意不同行内容的缩进距离。

(二)case支持glob风格的通配符


*: 任意长度任意字符
?: 任意单个字符

a|b: a或b

注意变量引用就是带$变量的名称。

如果第1个模式不匹配就继续执行第2个模式的代码。

*相当于else操作,表示的任意的字符串。

满足*就执行默认分支的操作。默认分支后面的两个分号可以不写的。

[root@centos7 ~]# type  case  case is a shell keyword  [root@centos7 ~]# help case case: case WORD in [PATTERN [| PATTERN]...) COMMANDS ;;]... esac        Execute commands based on pattern matching.          Selectively execute COMMANDS based upon WORD matching PATTERN.  The     `|' is used to separate multiple patterns.也就是表示或的关系          Exit Status:     Returns the status of the last command executed.

使用case写脚本判断变量是否是离散值

如果变量num的值是1,2,3那么就执行某个命令cmd1。

如果变量num的值是4,5,6那么就执行某个命令cmd2。

如果变量num的值是7,8,9那么就执行某个命令cmd3

否则就执行其他命令

[root@centos73 shell_scripts]# cat  case_1.sh  #!/bin/bash #Author=wang case $num in  1|2|3)        echo 1,2,3        ;; 4|5|6)        echo 4,5,6        ;; 7|8|9)        echo 7,8,9        ;; *)       echo other esac

没有对变量num赋值,所有都显示其他

[root@centos73 shell_scripts]# bash case_1.sh   4 other [root@centos73 shell_scripts]# bash case_1.sh   46 other [root@centos73 shell_scripts]# bash case_1.sh   1 other [root@centos73 shell_scripts]# bash case_1.sh   5 other [root@centos73 shell_scripts]# bash case_1.sh   7 other [root@centos73 shell_scripts]# bash case_1.sh   9 other [root@centos73 shell_scripts]# 

完整脚本

[root@centos73 shell_scripts]# cat   case_1.sh  #!/bin/bash #Author=wang read  -p "please input a num: " num case $num in  1|2|3)        echo 1,2,3        ;; 4|5|6)        echo 4,5,6        ;; 7|8|9)        echo 7,8,9        ;; *)       echo other esac

执行结果

[root@centos73 shell_scripts]# bash   case_1.sh    please input a num: 1 1,2,3 [root@centos73 shell_scripts]# bash   case_1.sh    please input a num: 3 1,2,3 [root@centos73 shell_scripts]# bash   case_1.sh    please input a num: 5 4,5,6 [root@centos73 shell_scripts]# bash   case_1.sh    please input a num: 7 7,8,9 [root@centos73 shell_scripts]# bash   case_1.sh    please input a num: 9 7,8,9 [root@centos73 shell_scripts]# bash   case_1.sh    please input a num: 11 other

判断yes或no

完整脚本

[root@centos73 shell_scripts]# cat   yesorno_case.sh #!/bin/bash #Author=wang read  -p  "Do you agree?(yes or no): "  ans case  $ans in [Yy]|[Yy][Ee][Ss])      echo yes      ;; [Nn]|[Nn][Oo])      echo no      ;; *) echo other esac

[root@centos73 shell_scripts]# bash  yesorno_case.sh Do you agree?(yes or no): Y yes [root@centos73 shell_scripts]# bash  yesorno_case.sh Do you agree?(yes or no): y yes [root@centos73 shell_scripts]# bash  yesorno_case.sh Do you agree?(yes or no): N no [root@centos73 shell_scripts]# bash  yesorno_case.sh Do you agree?(yes or no): n no

[root@centos73 shell_scripts]# bash  yesorno_case.sh Do you agree?(yes or no): yes yes [root@centos73 shell_scripts]# Yes -bash: Yes: command not found [root@centos73 shell_scripts]# bash  yesorno_case.sh Do you agree?(yes or no): Yes yes [root@centos73 shell_scripts]# bash  yesorno_case.sh Do you agree?(yes or no): YeS yes [root@centos73 shell_scripts]# bash  yesorno_case.sh Do you agree?(yes or no): YEs yes [root@centos73 shell_scripts]# bash  yesorno_case.sh Do you agree?(yes or no): yEs yes [root@centos73 shell_scripts]# bash  yesorno_case.sh Do you agree?(yes or no): yES yes [root@centos73 shell_scripts]# bash  yesorno_case.sh Do you agree?(yes or no): yeS yes

[root@centos73 shell_scripts]# bash  yesorno_case.sh Do you agree?(yes or no): no no [root@centos73 shell_scripts]# bash  yesorno_case.sh Do you agree?(yes or no): No no [root@centos73 shell_scripts]# bash  yesorno_case.sh Do you agree?(yes or no): NO       no [root@centos73 shell_scripts]# bash  yesorno_case.sh Do you agree?(yes or no): nO no

()表示和前面的分开,进行分组

而前面中括号里面的[Yy]是一定有的

?在正则表达式里面表示匹配其前面的字符0 或1次,也就是前面字符可有可无

[root@centos73 ~]# ans=yes;[[ $ans =~ ^[Yy]([Ee][Ss])?$ ]]  &&  echo  yes yes [root@centos73 ~]# ans=Yes;[[ $ans =~ ^[Yy]([Ee][Ss])?$ ]]  &&  echo  yes yes [root@centos73 ~]# ans=YEs;[[ $ans =~ ^[Yy]([Ee][Ss])?$ ]]  &&  echo  yes yes [root@centos73 ~]# ans=YES;[[ $ans =~ ^[Yy]([Ee][Ss])?$ ]]  &&  echo  yes yes [root@centos73 ~]# ans=YEs;[[ $ans =~ ^[Yy]([Ee][Ss])?$ ]]  &&  echo  yes yes [root@centos73 ~]# ans=yEs;[[ $ans =~ ^[Yy]([Ee][Ss])?$ ]]  &&  echo  yes yes

[root@centos73 ~]# ans=Y;[[ $ans =~ ^[Yy]([Ee][Ss])?$ ]]  &&  echo  yes yes [root@centos73 ~]# ans=y;[[ $ans =~ ^[Yy]([Ee][Ss])?$ ]]  &&  echo  yes yes

[root@centos73 ~]# ans=N;[[ $ans =~ ^[Nn]([Oo])?$ ]]  &&  echo  no no [root@centos73 ~]# ans=n;[[ $ans =~ ^[Nn]([Oo])?$ ]]  &&  echo  no no [root@centos73 ~]# ans=no;[[ $ans =~ ^[Nn]([Oo])?$ ]]  &&  echo  no no [root@centos73 ~]# ans=No;[[ $ans =~ ^[Nn]([Oo])?$ ]]  &&  echo  no no [root@centos73 ~]# ans=NO;[[ $ans =~ ^[Nn]([Oo])?$ ]]  &&  echo  no no [root@centos73 ~]# ans=nO;[[ $ans =~ ^[Nn]([Oo])?$ ]]  &&  echo  no no

[root@centos73 shell_scripts]# cat   yesorno_if.sh #!/bin/bash #Author=wang read -p "do you agree (yes/no):" choice yes="^[Yy]([Ee][Ss])?$" #()表示和前面的分开,进行分组 no="^[Nn]([Oo])?$" if [[ "$choice" =~ $yes ]] ; then     echo  "you enter yes" elif [[ "$choice" =~  $no ]] ; then     echo "you enter no " else     echo "enter not a yes or no " fi

[root@centos73 shell_scripts]# bash   yesorno_if.sh do you agree (yes/no):yes you enter yes [root@centos73 shell_scripts]# bash   yesorno_if.sh do you agree (yes/no):Y you enter yes [root@centos73 shell_scripts]# bash   yesorno_if.sh do you agree (yes/no):y you enter yes [root@centos73 shell_scripts]# bash   yesorno_if.sh do you agree (yes/no):N you enter no  [root@centos73 shell_scripts]# bash   yesorno_if.sh do you agree (yes/no):n you enter no  [root@centos73 shell_scripts]# bash   yesorno_if.sh do you agree (yes/no):nO you enter no  [root@centos73 shell_scripts]# bash   yesorno_if.sh do you agree (yes/no):YeS you enter yes [root@centos73 shell_scripts]# 

系统自带的函数是多层嵌套,尽量不要使用嵌套,否则就是给自己挖坑

[root@centos6 ~]# declare -f | less

  if [ "true" = "$(git rev-parse --is-inside-git-dir 2>/dev/null)" ]; then             if [ "true" = "$(git rev-parse --is-bare-repository 2>/dev/null)" ]; then                 c="BARE:";             else                 b="GIT_DIR!";             fi;         else             if [ "true" = "$(git rev-parse --is-inside-work-tree 2>/dev/null)" ]; then                 if [ -n "${GIT_PS1_SHOWDIRTYSTATE-}" ]; then                     if [ "$(git config --bool bash.showDirtyState)" != "false" ]; then                         git diff --no-ext-diff --quiet --exit-code || w="*";                         if git rev-parse --quiet --verify HEAD > /dev/null; then                             git diff-index --cached --quiet HEAD -- || i="+";                         else                             i="#";                         fi;                     fi;                 fi;

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!