有故障的开关盒? [英] Switch case with fallthrough?

查看:23
本文介绍了有故障的开关盒?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找 switch 语句的正确语法,在 Bash 中具有失败的情况(理想情况下不区分大小写).在 PHP 中我会这样编程:

I am looking for the correct syntax of the switch statement with fallthrough cases in Bash (ideally case-insensitive). In PHP I would program it like:

switch($c) {
    case 1:
        do_this();
        break;
     case 2:
     case 3:
        do_what_you_are_supposed_to_do();
        break;
     default:
        do_nothing(); 
}

我希望在 Bash 中也一样:

I want the same in 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 时触发.

This somehow doesn't work: function do_what_you_are_supposed_to_do() should be fired when $C is 2 OR 3.

推荐答案

对或"使用竖线(|).

case "$C" in
"1")
    do_this()
    ;;
"2" | "3")
    do_what_you_are_supposed_to_do()
    ;;
*)
    do_nothing()
    ;;
esac

这篇关于有故障的开关盒?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆