case语句
case语句为多选择语句。可以用case语句匹配一个值与一个模式,如果匹配成功,执行相匹配的命令。case语句格式如下:
- case 值 in
- 模式1 )
- 命令1
- . . .
- ;;
- 模式2 )
- 命令2
- . . .
- ;;
- esac
case 值 in
模式1 )
命令1
. . .
;;
模式2 )
命令2
. . .
;;
esac
case工作方式如上所示。取值后面必须为单词 in,每一模式必须以右括号结束。取值可以为变量或常数。匹配发现取值符合某一模式后,其间所有命令开始执行直至;;
取值将检测匹配的每一个模式。一旦模式匹配,则执行完匹配模式相应命令后不再继续其他模式。如果无一匹配模式,使用星号*捕获该值,再接受其他输入。模式部分可能包括元字符,与在命令行文件扩展名例子中使用过的匹配模式类型相同,即:
* |
任意字符。 |
? |
任意单字符。 |
[..] |
类或范围中任意字符。 |
来看一个简单的case语句:
- /home/l/g/tomotoboy >cat caseselect
- #!/bin/sh
- #caseselect
- echo - n "Enter a number from 1 to 5 :"
- read ANS
- case $ANS in
- 1) echo "you select 1"
- ;;
- 2) echo "you select 2"
- ;;
- 3) echo "you select 3"
- ;;
- 4) echo "you select 4"
- ;;
- 5) echo "you select 5"
- ;;
- *) echo "`basename $0`: This is not between 1 and 5" >&2
- ;;
- esac
-
- /home/l/g/tomotoboy >caseselect
- - n Enter a number from 1 to 5 :
- 4
- you select 4
- /home/l/g/tomotoboy >caseselect
- - n Enter a number from 1 to 5 :
- 7
- caseselect: This is not between 1 and 5
/home/l/g/tomotoboy >cat caseselect
#!/bin/sh
#caseselect
echo - n "Enter a number from 1 to 5 :"
read ANS
case $ANS in
1) echo "you select 1"
;;
2) echo "you select 2"
;;
3) echo "you select 3"
;;
4) echo "you select 4"
;;
5) echo "you select 5"
;;
*) echo "`basename $0`: This is not between 1 and 5" >&2
;;
esac
/home/l/g/tomotoboy >caseselect
- n Enter a number from 1 to 5 :
4
you select 4
/home/l/g/tomotoboy >caseselect
- n Enter a number from 1 to 5 :
7
caseselect: This is not between 1 and 5
对匹配模式使用|
- /home/l/g/tomotoboy >cat caseterm
- #!/bin/sh
- #caseterm
- echo "choices are.. vt100, vt102, vt220"
- echo -n "enter your terminal type :"
- read TERMINAL
- case $TERMINAL in
- vt100|vt102) TERM=vt100
- ;;
- vt220) TERM=vt220
- ;;
- *) echo "`basename $0`: Unknown reponse" >&2
- echo "setting it to vt100 anyway,so there"
- TERM=vt100
- ;;
- esac
- export TERM
- echo "Your terminal is set to $TERM'
- "caseterm" 17 lines, 348 characters
-
- /home/l/g/tomotoboy >caseterm
- choices are.. vt100, vt102, vt220
- -n enter your terminal type :
- vt100
- Your terminal is set to vt100'
/home/l/g/tomotoboy >cat caseterm
#!/bin/sh
#caseterm
echo "choices are.. vt100, vt102, vt220"
echo -n "enter your terminal type :"
read TERMINAL
case $TERMINAL in
vt100|vt102) TERM=vt100
;;
vt220) TERM=vt220
;;
*) echo "`basename $0`: Unknown reponse" >&2
echo "setting it to vt100 anyway,so there"
TERM=vt100
;;
esac
export TERM
echo "Your terminal is set to $TERM'
"caseterm" 17 lines, 348 characters
/home/l/g/tomotoboy >caseterm
choices are.. vt100, vt102, vt220
-n enter your terminal type :
vt100
Your terminal is set to vt100'
提示键入y或n
case的一个有效用法是提示用户响应以决定是否继续进程。这里提示输入y以继续处理n退出。如果用户输入Y、y或yes,处理继续执行case语句后面部分。如果用户输入N、n或no或其他响应,用户退出脚本。
- /home/l/g/tomotoboy >cat caseans
- #!/bin/sh
- #caseans
- echo -n "Do you wish to proceed [y..n]"
- read ANS
- case $ANS in
- y|Y|yes|Yes) echo "yes is selected"
- ;;
- n|N|no|No) echo "no is selectd"
- ;;
- *) echo "`basename $0` : Unknown response" >&2
- ;;
- esac
-
- /home/l/g/tomotoboy >caseans
- -n Do you wish to proceed [y..n]
- y
- yes is selected
/home/l/g/tomotoboy >cat caseans
#!/bin/sh
#caseans
echo -n "Do you wish to proceed [y..n]"
read ANS
case $ANS in
y|Y|yes|Yes) echo "yes is selected"
;;
n|N|no|No) echo "no is selectd"
;;
*) echo "`basename $0` : Unknown response" >&2
;;
esac
/home/l/g/tomotoboy >caseans
-n Do you wish to proceed [y..n]
y
yes is selected
case与命令参数传入
- /home/l/g/tomotoboy >cat >> caseparam
- #!/bin/sh
- #caseparam
- if [ $# != 1 ];then
- echo "Usage:`basename $0` [start|stop|help]" >&2
- fi
- OPT=$1
- case $OPT in
- start) echo "starting..`basename $0`"
- ;;
- stop) echo "stopping..`basename $0`"
- ;;
- help) echo "stopping..`basename $0`"
- echo "Usage:`basename $0` [start|stop|help]"
- ;;
- *) echo "stopping..`basename $0`"
- echo "Usage:`basename $0` [start|stop|help]"
- ;;
- esac
- /home/l/g/tomotoboy >chmod u+x caseparam
- /home/l/g/tomotoboy >caseparam help
- stopping..caseparam
- Usage:caseparam [start|stop|help]
- /home/l/g/tomotoboy >caseparam start
- starting..caseparam
/home/l/g/tomotoboy >cat >> caseparam
#!/bin/sh
#caseparam
if [ $# != 1 ];then
echo "Usage:`basename $0` [start|stop|help]" >&2
fi
OPT=$1
case $OPT in
start) echo "starting..`basename $0`"
;;
stop) echo "stopping..`basename $0`"
;;
help) echo "stopping..`basename $0`"
echo "Usage:`basename $0` [start|stop|help]"
;;
*) echo "stopping..`basename $0`"
echo "Usage:`basename $0` [start|stop|help]"
;;
esac
/home/l/g/tomotoboy >chmod u+x caseparam
/home/l/g/tomotoboy >caseparam help
stopping..caseparam
Usage:caseparam [start|stop|help]
/home/l/g/tomotoboy >caseparam start
starting..caseparam
捕获输入并执行空命令
不一定要在匹配模式后加入命令,如果你原本不想做什么,只是在进一步处理前过滤出意外响应,这样做是一种好办法。如果要运行对应于一个会计部门的帐目报表,必须首先在决定运行报表的类型前确认用户输入一个有效的部门号,匹配所有可能值,其他值无效。用case可以很容易实现上述功能。下面的脚本中如果用户输入部门号不是234、453、655或454,用户退出并返回可用信息。一旦响应了用户的有效部门号,脚本应用同样的技术取得报表类型,在case语句末尾显示有效的部门号和报表类型。脚本如下:
- #!/bin/sh
- # casevalid
- echo -n "enter the account dept No: "
- read ACC
- case $ACC in
- 234);;
- 453);;
- 655);;
- 454);;
- *) echo "basename $01`: Unknon dept No"
- echo "try..234,453,655,454"
- exit 1
- ;;
- esac
- #if we are here, then we have a validdated the dept no
- echo " 1 . post"
- echo " 2 . prior"
- echo -n "enter the type of report: "
- read ACC_TYPE
- case $ACC_TYPE in
- 1)TYPE=post;;
- 2)TYPE=prior;;
- *) echo "`basename $0`: Unknown account type." >&2
- "casevalid" 29 lines, 665 characters
- /home/l/g/tomotoboy >casevalid
- -n enter the account dept No:
- 454
- 1 . post
- 2 . prior
- -n enter the type of report:
- 1
- now running report for dept 454 for the type post