Mis*_*hka 176 bash switch-statement
我正在寻找switch语句的正确语法与Bash中的初级案例(理想情况下不区分大小写).在PHP中我会编程它像:
switch($c) {
case 1:
do_this();
break;
case 2:
case 3:
do_what_you_are_supposed_to_do();
break;
default:
do_nothing();
}
Run Code Online (Sandbox Code Playgroud)
我希望在Bash中也一样:
case "$C" in
"1")
do_this()
;;
"2")
"3")
do_what_you_are_supposed_to_do()
;;
*)
do_nothing();
;;
esac
Run Code Online (Sandbox Code Playgroud)
这在某种程度上是行不通的:do_what_you_are_supposed_to_do()当$ C为2或3时,应该触发函数.
gee*_*aur 286
使用竖条(|)表示"或".
case "$C" in
"1")
do_this()
;;
"2" | "3")
do_what_you_are_supposed_to_do()
;;
*)
do_nothing()
;;
esac
Run Code Online (Sandbox Code Playgroud)
Jas*_*sen 88
最新bash版本允许使用;&而不是;;:它们还允许通过使用来恢复案例检查;;&.
for n in 4 14 24 34
do
echo -n "$n = "
case "$n" in
3? )
echo -n thirty-
;;& #resume (to find ?4 later )
"24" )
echo -n twenty-
;& #fallthru
"4" | ?4)
echo -n four
;;& # resume ( to find teen where needed )
"14" )
echo -n teen
esac
echo
done
Run Code Online (Sandbox Code Playgroud)
样本输出
4 = four
14 = fourteen
24 = twenty-four
34 = thirty-four
Run Code Online (Sandbox Code Playgroud)
Spr*_*eak 25
()除非您想定义它们,否则不要在bash中使用函数名称.[23]匹配2或3''代替""如果括起来"",解释器(不必要地)尝试在匹配之前扩展值中的可能变量.
case "$C" in
'1')
do_this
;;
[23])
do_what_you_are_supposed_to_do
;;
*)
do_nothing
;;
esac
Run Code Online (Sandbox Code Playgroud)
对于不区分大小写的匹配,您可以使用字符类(如[23]):
case "$C" in
# will match C='Abra' and C='abra'
[Aa]'bra')
do_mysterious_things
;;
# will match all letter cases at any char like `abra`, `ABRA` or `AbRa`
[Aa][Bb][Rr][Aa])
do_wild_mysterious_things
;;
esac
Run Code Online (Sandbox Code Playgroud)
但是abra没有随时打,因为它将与第一个案例相匹配.
如果需要,您可以;;在第一种情况下省略在以下情况下继续测试匹配.(;;跳到esac)
Ren*_*amp 13
试试这个:
case $VAR in
normal)
echo "This doesn't do fallthrough"
;;
special)
echo -n "This does "
;&
fallthrough)
echo "fall-through"
;;
esac
Run Code Online (Sandbox Code Playgroud)
ras*_*hok 12
如果值是整数,那么您可以使用[2-3]或可以使用[5,7,8]非连续值.
#!/bin/bash
while [ $# -gt 0 ];
do
case $1 in
1)
echo "one"
;;
[2-3])
echo "two or three"
;;
[4-6])
echo "four to six"
;;
[7,9])
echo "seven or nine"
;;
*)
echo "others"
;;
esac
shift
done
Run Code Online (Sandbox Code Playgroud)
如果值是字符串,那么您可以使用|.
#!/bin/bash
while [ $# -gt 0 ];
do
case $1 in
"one")
echo "one"
;;
"two" | "three")
echo "two or three"
;;
*)
echo "others"
;;
esac
shift
done
Run Code Online (Sandbox Code Playgroud)