一case条件语句
(一)case语句的语法
适合判断变量值
case 变量引用 in
PAT1/值1)
分支1/指令1
;;
PAT2/值2)
分支2/指令2
;;
...
*)
默认分支
;;
esac
(二)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
使用正则表达式判断yes或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;