shell中的case语句:
可以把变量的内容与多个模板进行匹配,再根据成功匹配的模板去决定应该执行哪部分代码。
使用格式:
case 匹配母板 in
模板1 [ | 模板2 ] … ) 语句组 ;;
模板3 [ | 模板4 ] … ) 语句组 ;;
esac
case语句的匹配是从上往下地匹配顺序。因此,case语句编写的原则是从上往下,模板从特殊到普通。在C语言里,case语句中有default模板,而在shell程序设计中,可能将模板写成*,就可以完成相同的功能。
例1:
#!/bin/sh
echo "Please input \"yes\" or \"no\""
read var
case "$var" in
[yY][eE][sS] ) echo "Your input is YES" ;;
[nN][oO] ) echo "Your input is no" ;;
* ) echo "Input Error!" ;;
esac
exit 0
例2:
#!/bin/bash
FRUIT=$1 #注意没有空格
if [ -z "$1" ] #中括号跟 -z 要有空格
then
echo "没有在第一个参数输入水果"
else
case "$FRUIT" in
apple|APPLE) echo "THE FRUIT is apple";;
banana|BANANA) echo "THE FRULT is banana";;
#可以使用*)来表示默认选项,注意最后要用两个分号来结尾
*) echo "输入的是不是我要的水果啊?";;
esac
fi
#--- $1既是外部传参的值,如:运行该脚本的时候:./test.sh apple
#---此处apple既是$1
附另外一个示例:
#!/bin/bash
HOSTS=" nss.sh cs.sh SS.sh ds_handler2 tomcat blog httpd "
for myHost in $HOSTS
do
count=(`ps aux |grep -v grep |grep $myHost |wc -l`)
echo $myHost
echo $count
done
相关推荐
其中:case in 和esac都是shell关键字,expression表示表达式,pattern表示匹配模式 expression既可以是一个变量、一个数字、一个字符串,也可以是一个数学计算表达式或者命令的执行结果 pattern可以是一个数字、一...
shell中的 case in 用法 用法 case;in;esac都是shell关键字 expression是表达式(既可以是一个变量,一个数字,一个字符串,还可以是一个数学计算表达式,或者是命令的执行结果,只要可以得到expression的值就行) ...
主要介绍了Shell编程之case语句实战(小结),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
shell支持两种分支结构,分别为if else 与case in语句。 基本格式为: case expression in pattern1) statement1 ;; pattern2) statement2 ;; ........ ;; *) statement esac case会将expression 的值与...
Shell case语句为多选择语句。可以用case语句匹配一个值与一个模式,如果匹配成功,执行相匹配的命令。case语句格式如下: 代码如下: case 值 in 模式1) command1 command2 … commandN ;; 模式2) ...
Shell编程-流程控制-case语句
使用格式:case 匹配母板 in模板1 [ | 模板2 ] … ) 语句组 ;;模板3 [ | 模板4 ] … ) 语句组 ;;esaccase语句的匹配是从上往下地匹配顺序。因此,case语句编写的原则是从上往下,模板从特殊到普通。在C语言里,case...
在shell脚本中,发现case语句的一个问题。就是指定小写字母[a-z]和大写字母[A-Z]的这种方法不管用了。 出现如下情况: 代码如下:[root@station1 ~]# cat case.sh#!/bin/bashwhile :doecho -n “input a letter: ...
47 #+ of hours in my case, so I really want to avoid this]. Also, as search 48 #+ always starts again from the beginning, one can encode priorities in 49 #+ the file names. Of course, one could ...
和其它编程语言类似,Shell 也支持两种分支结构(选择结构),分别是 if else 语句和 case in 语句。 当分支较多,并且判断条件比较简单时,使用 case in 语句就比较方便了 case in 的用法基本格式如下: case ...
Each real-world example recipe follows the same structure and easily shows you what's going on in each case. File conversion (DOS, UNIX, and Mac), system administration, and resource monitoring are ...
case $choice in 1) ls;; 2) echo "enter target directory:" read dir cd $dir ;; 3) echo "enter file name:" read file vi $file ;; 4) echo "enter file name:" read file rm $file ;; 5) echo ...
Each real-world example recipe follows the same structure and easily shows you what's going on in each case. File conversion (DOS, UNIX, and Mac), system administration, and resource monitoring are ...
4. case语句:case value in pattern) commands;; esac,例如case $i in 1) echo "Monday";; 2) echo "Tuesday";; esac。 六、函数的基本格式 函数是shell脚本中的一种代码块,可以重复使用,函数的基本格式为: ...
shell编程中的case语句也是这个意思,case会对字符串进行匹配,是从第一个模式开始的,如果有一个模式已经匹配成功的话,其他的模式就不会再进行匹配了,下面看下代码中的用法吧。 #!/bin/sh echo please yes or no...
一、shell条件语句(if用法) if语句结构[if/then/elif/else/fi] 代码如下:if 条件测试语句 then action [elif 条件 action else action ] fi如果对于:条件测试语句不是很清楚,可以参考:linux shell 逻辑运算符...
#前言:这篇我们接着写shell的另外一个条件语句case,上篇讲解了if条件语句。case条件语句我们常用于实现系统服务启动脚本等场景,case条件语句也相当于if条件语句多分支结构,多个选择,case看起来更规范和易读 #...
shell. Then, you'll learn how to write a simple bash script and how to edit your bash script using Linux editors. Following this, you will learn how to define a variable and the visibility of a ...
shell循环 shell的流程控制 if select switch for in case