我正在寻找 Bash 中带有 fallthrough cases 的 switch 语句的正确语法(最好不区分大小写)。 在 PHP 中,我会像这样编程:
switch($c) {
case 1:
do_this();
break;
case 2:
case 3:
do_what_you_are_supposed_to_do();
break;
default:
do_nothing();
}
我想在 Bash 中使用同样的东西:
case "$C" in
"1")
do_this()
;;
"2")
"3")
do_what_you_are_supposed_to_do()
;;
*)
do_nothing();
;;
esac
这在某种程度上不起作用:函数 do_what_you_are_supposed_to_do()
应该在 $C 为 2 或 3 时触发。
最佳答案
使用竖线 (|
) 表示“或”。
case "$C" in
"1")
do_this()
;;
"2" | "3")
do_what_you_are_supposed_to_do()
;;
*)
do_nothing()
;;
esac
关于bash - 切换案例与 fallthrough?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5562253/