在实际编程中,我们有时会请到多条件多分支选择的情况,用if…else语句来嵌套处理不烦琐,于是JavaScript等语言提供了多选择语句switch ... case。与此类似,Linux Shell脚本编程中提供了case...in...esac 语句,来实现多条件多分支选择结构:其语法结构为:
case 变量名 in
值1[|值2……])
命令1
[...]
[命令N]
;;[...]
值a[|值b……])
命令a
[...]
[命令b]
[break]
;;[*)]
[命令x]
[...]
[命令y]
esac
在每个 case 分支语句中,首先列出该分支对应的值(多个值之间用|分隔)并以半角右圆括号开始,然后列出要执行的语句或命令序列,最后用两个分号 ;; 表示该分支结束。
在把我们想处理的值列出来后,对于其它情况,在C和JavaScript等语言的switch语句中是用default:来表示的,在Matlab等语言的switch语句中是用otherwise来表示,在Linux shell编程中,是用下面的格式来表示的:
*)
case语句以 esac 结束,esac就是case的反序,这与if语句以fi结束相类。
其中的[]的内容是可选的。
在很多编程语言中,为了实现在循环过程中,有时候需要在未达到循环结束条件时强制跳出循环而提供了break语句,Linux Shell脚本编程同样提供了break语句。
为了演示case ... esac、和break语句的功能和用法,我们将创建一个演示脚本文件:
while :
do
echo -n "Please enter a nummber or letter: "
read c
case $c in
0|1|2|3|4|5|6|7|8|9) echo "You entered a nammber:$c"
;;
a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z) echo "You entered a lowercase letter: $c. Contine."
;;
A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z) echo "You entered a capital letter:$c. Contine."
;;
*) echo "You entered another character:$c \nGame over!"
break;
;;
esac
done
脚本文件的功能是:
通过while : 语句执行一个无限循环
在循环体中:
首先 用echo -n 命令显示提示信息:"Please enter a nummber or letter: ",其中 -n选项的作用是显示信息后不自动换行。
接着用read命令读取一个字符保存到变量c中
然后用case 语句对变量c保存的用户输入字符进行判断:
下面我们分别在zsh和bash中测试
我们用 echo 和 输出重定向来创建。
# csdn @ edu in ~ [20:01:21]
$ echo 'while : ' > a.sh# csdn @ edu in ~ [20:01:52]
$ echo ' do' >> a.sh# csdn @ edu in ~ [20:01:52]
$ echo ' echo -n "Please enter a nummber or letter: "' >> a.sh# csdn @ edu in ~ [20:01:52]
$ echo ' read c' >> a.sh# csdn @ edu in ~ [20:01:52]
$ echo ' case $c in ' >> a.sh# csdn @ edu in ~ [20:01:52]
$ echo ' 0|1|2|3|4|5|6|7|8|9) echo "You entered a nammber:$c"' >> a.sh# csdn @ edu in ~ [20:01:52]
$ echo ' ;;' >> a.sh# csdn @ edu in ~ [20:01:52]
$ echo ' a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z) echo "You entered a lowercase letter: $c. Continue."' >> a.sh# csdn @ edu in ~ [20:01:52]
$ echo ' ;;' >> a.sh# csdn @ edu in ~ [20:01:52]
$ echo ' A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z) echo "You entered a capital letter:$c. Continue." ' >> a.sh# csdn @ edu in ~ [20:01:52]
$ echo ' ;;' >> a.sh# csdn @ edu in ~ [20:01:52]
$ echo ' *) echo "You entered another character:$c \\nGame over!"' >> a.sh# csdn @ edu in ~ [20:01:52]
$ echo ' break;' >> a.sh# csdn @ edu in ~ [20:01:52]
$ echo ' ;;' >> a.sh# csdn @ edu in ~ [20:01:52]
$ echo ' esac' >> a.sh# csdn @ edu in ~ [20:01:52]
$ echo 'done' >> a.sh# csdn @ edu in ~ [20:01:52]
$
# csdn @ edu in ~ [20:05:43]
$ cat a.sh
while :
do
echo -n "Please enter a nummber or letter: "
read c
case $c in
0|1|2|3|4|5|6|7|8|9) echo "You entered a nammber:$c"
;;
a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z) echo "You entered a lowercase letter: $c. Continue."
;;
A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z) echo "You entered a capital letter:$c. Continue."
;;
*) echo "You entered another character:$c \nGame over!"
break;
;;
esac
done# csdn @ edu in ~ [20:05:47]
$
# csdn @ edu in ~ [20:05:47]
$ . ./a.sh
Please enter a nummber or letter: 3
You entered a nammber:3
Please enter a nummber or letter: 9
You entered a nammber:9
Please enter a nummber or letter: A
You entered a capital letter:A. Continue.
Please enter a nummber or letter: x
You entered a lowercase letter: x. Continue.
Please enter a nummber or letter: b
You entered a lowercase letter: b. Continue.
Please enter a nummber or letter: Z
You entered a capital letter:Z. Continue.
Please enter a nummber or letter: D
You entered a capital letter:D. Continue.
Please enter a nummber or letter: *
You entered another character:*
Game over!# csdn @ edu in ~ [20:07:07]
$
我们依次输入字符3、9、A、x、b、z、D和*,当输入的字符是数字或字母时,循环继续,直到我们输入*,才跳出循环。
# csdn @ edu in ~ [20:07:07]
$ exec bash
[csdn ~]$ echo 'while : ' > a.sh
[csdn ~]$ echo ' do' >> a.sh
[csdn ~]$ echo ' echo -n "Please enter a nummber or letter: "' >> a.sh
[csdn ~]$ echo ' read c' >> a.sh
[csdn ~]$ echo ' case $c in ' >> a.sh
[csdn ~]$ echo ' 0|1|2|3|4|5|6|7|8|9) echo "You entered a nammber:$c"' >> a.sh
[csdn ~]$ echo ' ;;' >> a.sh
[csdn ~]$ echo ' a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z) echo "You entered a lowercase letter: $c. Continue."' >> a.sh
[csdn ~]$ echo ' ;;' >> a.sh
[csdn ~]$ echo ' A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z) echo "You entered a capital letter:$c. Continue." ' >> a.sh
[csdn ~]$ echo ' ;;' >> a.sh
[csdn ~]$ echo ' *) echo "You entered another character:$c \\nGame over!"' >> a.sh
[csdn ~]$ echo ' break;' >> a.sh
[csdn ~]$ echo ' ;;' >> a.sh
[csdn ~]$ echo ' esac' >> a.sh
[csdn ~]$ echo 'done' >> a.sh
[csdn ~]$ cat a.sh
while :
do
echo -n "Please enter a nummber or letter: "
read c
case $c in
0|1|2|3|4|5|6|7|8|9) echo "You entered a nammber:$c"
;;
a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z) echo "You entered a lowercase letter: $c. Continue."
;;
A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z) echo "You entered a capital letter:$c. Continue."
;;
*) echo "You entered another character:$c \\nGame over!"
break;
;;
esac
done
[csdn ~]$
[csdn ~]$ . a.sh
Please enter a nummber or letter: 4
You entered a nammber:4
Please enter a nummber or letter: A
You entered a capital letter:A. Continue.
Please enter a nummber or letter: d
You entered a lowercase letter: d. Continue.
Please enter a nummber or letter: D
You entered a capital letter:D. Continue.
Please enter a nummber or letter: /
You entered another character:/ \nGame over!
[csdn ~]$
我们依次输入字符4、A、d、D和/,当输入的字符是数字或字母时,循环继续,直到我们输入/,才跳出循环。
通过上面的实例,可以看到:
1.zsh和bash对case ... esac和break语句都提供了支持。
2.;zsh和bash对字符串"\\n"的处理是不同的。如:
echo ' *) echo "You entered another character:$c \\nGame over!"' >> a.sh
在zsh中生成的语句是:
*) echo "You entered another character:$c \nGame over!"
在bash中生成的语句是:
*) echo "You entered another character:$c \\nGame over!"
这也导致zsh和bas在显示该行信息时存在一点差异。
为了测试不同的shell中对break语句功能的支持程度是否存在不同。
我们在实例1的外面再加上一个while : 无限循环,看看用break语句能跳出几重循环。
# csdn @ edu in ~ [19:00:42]
$ echo 'while : ' > a.sh# csdn @ edu in ~ [19:01:08]
$ echo 'do' >> a.sh# csdn @ edu in ~ [19:01:08]
$ echo 'while : ' >> a.sh# csdn @ edu in ~ [19:01:08]
$ echo ' do' >> a.sh# csdn @ edu in ~ [19:01:08]
$ echo ' echo -n "Please enter a nummber or letter: "' >> a.sh# csdn @ edu in ~ [19:01:08]
$ echo ' read c' >> a.sh# csdn @ edu in ~ [19:01:08]
$ echo ' case $c in ' >> a.sh# csdn @ edu in ~ [19:01:08]
$ echo ' 0|1|2|3|4|5|6|7|8|9) echo "You entered a nammber:$c"' >> a.sh# csdn @ edu in ~ [19:01:08]
$ echo ' ;;' >> a.sh# csdn @ edu in ~ [19:01:08]
$ echo ' a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z) echo "You entered a lowercase letter: $c. Continue."' >> a.sh# csdn @ edu in ~ [19:01:08]
$ echo ' ;;' >> a.sh# csdn @ edu in ~ [19:01:08]
$ echo ' A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z) echo "You entered a capital letter:$c. Continue." ' >> a.sh# csdn @ edu in ~ [19:01:08]
$ echo ' ;;' >> a.sh# csdn @ edu in ~ [19:01:08]
$ echo ' *) echo "You entered another character:$c \\nGame over!"' >> a.sh# csdn @ edu in ~ [19:01:08]
$ echo ' break;' >> a.sh# csdn @ edu in ~ [19:01:08]
$ echo ' ;;' >> a.sh# csdn @ edu in ~ [19:01:08]
$ echo ' esac' >> a.sh# csdn @ edu in ~ [19:01:08]
$ echo 'done' >> a.sh# csdn @ edu in ~ [19:01:08]
# csdn @ edu in ~ [19:02:56]
$ cat a.sh
while :
do
while :
do
echo -n "Please enter a nummber or letter: "
read c
case $c in
0|1|2|3|4|5|6|7|8|9) echo "You entered a nammber:$c"
;;
a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z) echo "You entered a lowercase letter: $c. Continue."
;;
A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z) echo "You entered a capital letter:$c. Continue."
;;
*) echo "You entered another character:$c \nGame over!"
break;
;;
esac
done
done# csdn @ edu in ~ [19:03:56]
$
# csdn @ edu in ~ [19:04:41] C:127
$ . ./a.sh
Please enter a nummber or letter: 3
You entered a nammber:3
Please enter a nummber or letter: 9
You entered a nammber:9
Please enter a nummber or letter: d
You entered a lowercase letter: d. Continue.
Please enter a nummber or letter: X
You entered a capital letter:X. Continue.
Please enter a nummber or letter: Y
You entered a capital letter:Y. Continue.
Please enter a nummber or letter: *
You entered another character:*
Game over!
Please enter a nummber or letter: ^C%# csdn @ edu in ~ [19:06:06] C:126
$
[csdn ~]$ echo 'while : ' > a.sh
[csdn ~]$ echo 'do' >> a.sh
[csdn ~]$ echo 'while : ' >> a.sh
[csdn ~]$ echo ' do' >> a.sh
[csdn ~]$ echo ' echo -n "Please enter a nummber or letter: "' >> a.sh
[csdn ~]$ echo ' read c' >> a.sh
[csdn ~]$ echo ' case $c in ' >> a.sh
[csdn ~]$ echo ' 0|1|2|3|4|5|6|7|8|9) echo "You entered a nammber:$c"' >> a.sh
[csdn ~]$ echo ' ;;' >> a.sh
[csdn ~]$ echo ' a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z) echo "You entered a lowercase letter: $c. Continue."' >> a.sh
[csdn ~]$ echo ' ;;' >> a.sh
[csdn ~]$ echo ' A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z) echo "You entered a capital letter:$c. Continue." ' >> a.sh
[csdn ~]$ echo ' ;;' >> a.sh
[csdn ~]$ echo ' *) echo "You entered another character:$c \\nGame over!"' >> a.sh
[csdn ~]$ echo ' break;' >> a.sh
[csdn ~]$ echo ' ;;' >> a.sh
[csdn ~]$ echo ' esac' >> a.sh
[csdn ~]$ echo 'done' >> a.sh
[csdn ~]$ echo 'done' >> a.sh
[csdn ~]$ cat a.sh
while :
do
while :
do
echo -n "Please enter a nummber or letter: "
read c
case $c in
0|1|2|3|4|5|6|7|8|9) echo "You entered a nammber:$c"
;;
a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z) echo "You entered a lowercase letter: $c. Continue."
;;
A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z) echo "You entered a capital letter:$c. Continue."
;;
*) echo "You entered another character:$c \\nGame over!"
break;
;;
esac
done
done
[csdn ~]$
[csdn ~]$ . a.sh
Please enter a nummber or letter: 6
You entered a nammber:6
Please enter a nummber or letter: a
You entered a lowercase letter: a. Continue.
Please enter a nummber or letter: B
You entered a capital letter:B. Continue.
Please enter a nummber or letter: *
You entered another character:* \nGame over!
Please enter a nummber or letter: -
You entered another character:- \nGame over!
Please enter a nummber or letter: ^C
[csdn ~]$
从上面的测试结果来看,无论是在zsh中,还是在bash中,break都只能跳出一层循环。
所以在上面的例子中,我们使用^C来终止脚本执行。
linux shell 脚本 编程中的continue语句的作用,跟C、JavaScript等其它编程语言一样,就是跳转,重新开始循环。
为了验证continue语句的作用,我对实例1中最后一个case分支的代码:
*) echo "You entered another character:$c \nGame over!"
break;
;;
改为:
*) echo "You entered another character:$c"
continue
echo "Game over!"
break;
;;
即:
while :
do
echo -n "Please enter a nummber or letter: "
read c
case $c in
0|1|2|3|4|5|6|7|8|9) echo "You entered a nammber:$c"
;;
a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z) echo "You entered a lowercase letter: $c. Contine."
;;
A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z) echo "You entered a capital letter:$c. Contine."
;;
*) echo "You entered another character:$c"
continue
echo "Game over!"
break;
;;
esac
done
这样,当我们输入一个非数字或字母字符时,看看会代码执行结果发生什么样的变化?
echo 'while : ' > a.sh
echo ' do' >> a.sh
echo ' echo -n "Please enter a nummber or letter: "' >> a.sh
echo ' read c' >> a.sh
echo ' case $c in ' >> a.sh
echo ' 0|1|2|3|4|5|6|7|8|9) echo "You entered a nammber:$c"' >> a.sh
echo ' ;;' >> a.sh
echo ' a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z) echo "You entered a lowercase letter: $c. Continue."' >> a.sh
echo ' ;;' >> a.sh
echo ' A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z) echo "You entered a capital letter:$c. Continue." ' >> a.sh
echo ' ;;' >> a.sh
echo ' *) echo "You entered another character:$c"' >> a.sh
echo ' continue' >> a.sh
echo ' echo "Game over!"' >> a.sh
echo ' break;' >> a.sh
echo ' ;;' >> a.sh
echo ' esac' >> a.sh
echo 'done' >> a.sh
# csdn @ edu in ~ [21:18:17] C:126
$ cat a.sh
while :
do
echo -n "Please enter a nummber or letter: "
read c
case $c in
0|1|2|3|4|5|6|7|8|9) echo "You entered a nammber:$c"
;;
a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z) echo "You entered a lowercase letter: $c. Continue."
;;
A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z) echo "You entered a capital letter:$c. Continue."
;;
*) echo "You entered another character:$c"
continue
echo "Game over!"
break;
;;
esac
done# csdn @ edu in ~ [21:19:29]
$
# csdn @ edu in ~ [21:16:42]
$ . ./a.sh
Please enter a nummber or letter: b
You entered a lowercase letter: b. Continue.
Please enter a nummber or letter: 1
You entered a nammber:1
Please enter a nummber or letter: c
You entered a lowercase letter: c. Continue.
Please enter a nummber or letter: *
You entered another character:*
Please enter a nummber or letter: ^C%# csdn @ edu in ~ [21:18:17] C:126
$
# csdn @ edu in ~ [23:25:46]
$ exec bash[csdn ~]$ echo 'while : ' > a.sh
[csdn ~]$ echo ' do' >> a.sh
[csdn ~]$ echo ' echo -n "Please enter a nummber or letter: "' >> a.sh
[csdn ~]$ echo ' read c' >> a.sh
[csdn ~]$ echo ' case $c in ' >> a.sh
[csdn ~]$ echo ' 0|1|2|3|4|5|6|7|8|9) echo "You entered a nammber:$c"' >> a.sh
[csdn ~]$ echo ' ;;' >> a.sh
[csdn ~]$ echo ' a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z) echo "You entered a lowercase letter: $c. Continue."' >> a.sh
[csdn ~]$ echo ' ;;' >> a.sh
[csdn ~]$ echo ' A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z) echo "You entered a capital letter:$c. Continue." ' >> a.sh
[csdn ~]$ echo ' ;;' >> a.sh
[csdn ~]$ echo ' *) echo "You entered another character:$c"' >> a.sh
[csdn ~]$ echo ' continue' >> a.sh
[csdn ~]$ echo ' echo "Game over!"' >> a.sh
[csdn ~]$ echo ' break;' >> a.sh
[csdn ~]$ echo ' ;;' >> a.sh
[csdn ~]$ echo ' esac' >> a.sh
[csdn ~]$ echo 'done' >> a.sh
2.用cat命令查看脚本文件的内容
[csdn ~]$ cat a.sh
while :
do
echo -n "Please enter a nummber or letter: "
read c
case $c in
0|1|2|3|4|5|6|7|8|9) echo "You entered a nammber:$c"
;;
a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z) echo "You entered a lowercase letter: $c. Continue."
;;
A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z) echo "You entered a capital letter:$c. Continue."
;;
*) echo "You entered another character:$c"
continue
echo "Game over!"
break;
;;
esac
done
[csdn ~]$
[csdn ~]$ . a.sh
Please enter a nummber or letter: 3
You entered a nammber:3
Please enter a nummber or letter: a
You entered a lowercase letter: a. Continue.
Please enter a nummber or letter: C
You entered a capital letter:C. Continue.
Please enter a nummber or letter: *
bash: echo: command not found
Please enter a nummber or letter: ^C
[csdn ~]$
在上面的实例中,当我们输入字符*时,程序执行代码:
*) echo "You entered another character:$c"
输出信息:
You entered another character:*
后,就遇到 了continue语句,重新开始循环。
continue语句后面的代码:
echo "Game over!"
break;
没有执行。
所以我们还是要用Ctrl+C来终止脚本的运行。