bash - 切换案例与 fallthrough?

标签 bash switch-statement

我正在寻找 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/

相关文章:

javascript - Node : Refactoring a long switch statement

Linux进程重启器

linux - 连接 echo 的结果和命令输出

linux - 我什么时候应该使用 ""在 shell 测试和 echo 中引用一个值?

c++ - 在 C++ 中访问由 Bash 进程替换创建的 'virtual file'

javascript - 当 AngularJS 的 bool 值为 TRUE 时显示图像(或图标)?

c - 使用 scanf() 和 switch 语句计算字符串中的字符数

bash - 如何在一分钟后在 linux 中启动 shell 脚本?

java - 我如何返回到之前的菜单?

java - 替换 Java 的 switch 语句