case语句
Posted wang618
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了case语句相关的知识,希望对你有一定的参考价值。
一case条件语句
(一)case语句的语法
适合判断变量值
case 变量引用 in
PAT1/值1)
分支1/指令1
;;
PAT2/值2)
分支2/指令2
;;
...
*)
默认分支
;;
esac
(二)case支持glob风格的通配符
*: 任意长度任意字符
?: 任意单个字符
[ ]:指定范围内的任意单个字符
a|b: a或b
注意变量引用就是带$变量的名称。
如果第1个模式不匹配就继续执行第2个模式的代码。
*相当于else操作,表示的任意的字符串。
满足*就执行默认分支的操作。默认分支后面的两个分号可以不写的。
[[email protected] ~]# type case case is a shell keyword [[email protected] ~]# 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
否则就执行其他命令
[[email protected] 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赋值,所有都显示其他
[[email protected] shell_scripts]# bash case_1.sh 4 other [[email protected] shell_scripts]# bash case_1.sh 46 other [[email protected] shell_scripts]# bash case_1.sh 1 other [[email protected] shell_scripts]# bash case_1.sh 5 other [[email protected] shell_scripts]# bash case_1.sh 7 other [[email protected] shell_scripts]# bash case_1.sh 9 other [[email protected] shell_scripts]#
完整脚本
[[email protected] 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
执行结果
[[email protected] shell_scripts]# bash case_1.sh please input a num: 1 1,2,3 [[email protected] shell_scripts]# bash case_1.sh please input a num: 3 1,2,3 [[email protected] shell_scripts]# bash case_1.sh please input a num: 5 4,5,6 [[email protected] shell_scripts]# bash case_1.sh please input a num: 7 7,8,9 [[email protected] shell_scripts]# bash case_1.sh please input a num: 9 7,8,9 [[email protected] shell_scripts]# bash case_1.sh please input a num: 11 other
判断yes或no
完整脚本
[[email protected] 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
执行结果
[[email protected] shell_scripts]# bash yesorno_case.sh Do you agree?(yes or no): Y yes [[email protected] shell_scripts]# bash yesorno_case.sh Do you agree?(yes or no): y yes [[email protected] shell_scripts]# bash yesorno_case.sh Do you agree?(yes or no): N no [[email protected] shell_scripts]# bash yesorno_case.sh Do you agree?(yes or no): n no
[[email protected] shell_scripts]# bash yesorno_case.sh Do you agree?(yes or no): yes yes [[email protected] shell_scripts]# Yes -bash: Yes: command not found [[email protected] shell_scripts]# bash yesorno_case.sh Do you agree?(yes or no): Yes yes [[email protected] shell_scripts]# bash yesorno_case.sh Do you agree?(yes or no): YeS yes [[email protected] shell_scripts]# bash yesorno_case.sh Do you agree?(yes or no): YEs yes [[email protected] shell_scripts]# bash yesorno_case.sh Do you agree?(yes or no): yEs yes [[email protected] shell_scripts]# bash yesorno_case.sh Do you agree?(yes or no): yES yes [[email protected] shell_scripts]# bash yesorno_case.sh Do you agree?(yes or no): yeS yes
[[email protected] shell_scripts]# bash yesorno_case.sh Do you agree?(yes or no): no no [[email protected] shell_scripts]# bash yesorno_case.sh Do you agree?(yes or no): No no [[email protected] shell_scripts]# bash yesorno_case.sh Do you agree?(yes or no): NO no [[email protected] shell_scripts]# bash yesorno_case.sh Do you agree?(yes or no): nO no
使用正则表达式判断yes或no
()表示和前面的分开,进行分组
而前面中括号里面的[Yy]是一定有的
?在正则表达式里面表示匹配其前面的字符0 或1次,也就是前面字符可有可无
[[email protected] ~]# ans=yes;[[ $ans =~ ^[Yy]([Ee][Ss])?$ ]] && echo yes yes [[email protected] ~]# ans=Yes;[[ $ans =~ ^[Yy]([Ee][Ss])?$ ]] && echo yes yes [[email protected] ~]# ans=YEs;[[ $ans =~ ^[Yy]([Ee][Ss])?$ ]] && echo yes yes [[email protected] ~]# ans=YES;[[ $ans =~ ^[Yy]([Ee][Ss])?$ ]] && echo yes yes [[email protected] ~]# ans=YEs;[[ $ans =~ ^[Yy]([Ee][Ss])?$ ]] && echo yes yes [[email protected] ~]# ans=yEs;[[ $ans =~ ^[Yy]([Ee][Ss])?$ ]] && echo yes yes
[[email protected] ~]# ans=Y;[[ $ans =~ ^[Yy]([Ee][Ss])?$ ]] && echo yes yes [[email protected] ~]# ans=y;[[ $ans =~ ^[Yy]([Ee][Ss])?$ ]] && echo yes yes
[[email protected] ~]# ans=N;[[ $ans =~ ^[Nn]([Oo])?$ ]] && echo no no [[email protected] ~]# ans=n;[[ $ans =~ ^[Nn]([Oo])?$ ]] && echo no no [[email protected] ~]# ans=no;[[ $ans =~ ^[Nn]([Oo])?$ ]] && echo no no [[email protected] ~]# ans=No;[[ $ans =~ ^[Nn]([Oo])?$ ]] && echo no no [[email protected] ~]# ans=NO;[[ $ans =~ ^[Nn]([Oo])?$ ]] && echo no no [[email protected] ~]# ans=nO;[[ $ans =~ ^[Nn]([Oo])?$ ]] && echo no no
完整脚本
[[email protected] 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
执行结果
[[email protected] shell_scripts]# bash yesorno_if.sh do you agree (yes/no):yes you enter yes [[email protected] shell_scripts]# bash yesorno_if.sh do you agree (yes/no):Y you enter yes [[email protected] shell_scripts]# bash yesorno_if.sh do you agree (yes/no):y you enter yes [[email protected] shell_scripts]# bash yesorno_if.sh do you agree (yes/no):N you enter no [[email protected] shell_scripts]# bash yesorno_if.sh do you agree (yes/no):n you enter no [[email protected] shell_scripts]# bash yesorno_if.sh do you agree (yes/no):nO you enter no [[email protected] shell_scripts]# bash yesorno_if.sh do you agree (yes/no):YeS you enter yes [[email protected] shell_scripts]#
系统自带的函数是多层嵌套,尽量不要使用嵌套,否则就是给自己挖坑
[[email protected] ~]# 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;
以上是关于case语句的主要内容,如果未能解决你的问题,请参考以下文章