简体   繁体   English

bash 在使用 fallthrough 时是否有与 case 语句等效的“default”?

[英]Does bash have a `default`-equivalent for the case statement when using fallthrough?

case "$action" in
a|b)
    echo for a or b
;;&
b|c)
    echo for c or b
;;&
*)
    echo for everything ELSE
;;&
esac

So, as you can see, I'm using ;;& instead of ;;所以,正如你所看到的,我使用;;&而不是;; so that if action=b it will trigger both of the first two cases.所以如果action=b它将触发前两种情况。
However, a drawback of this is that *) no longer 'means' "everything else", but will match "everything" instead;然而,这样做的一个缺点是*)不再“意味着”“其他一切”,而是会匹配“一切”; thus b will trigger the final one also.因此b也将触发最后一个。

PowerShell is able to do what I want because it has a dedicated default keyword, does Bash have something similar? PowerShell 能够做我想做的事,因为它有一个专用的default关键字,Bash 有没有类似的东西?
What about an exhaustive work-around like [!(a|b|c)]) or something?[!(a|b|c)])类的详尽解决方法呢?

It would have been handy to do something like case 5 in 4) echo bob; ;;& esac || echo DEFAULT case 5 in 4) echo bob; ;;& esac || echo DEFAULT执行类似case 5 in 4) echo bob; ;;& esac || echo DEFAULT操作会很方便case 5 in 4) echo bob; ;;& esac || echo DEFAULT case 5 in 4) echo bob; ;;& esac || echo DEFAULT case 5 in 4) echo bob; ;;& esac || echo DEFAULT but case doesn't seem to return any code. case 5 in 4) echo bob; ;;& esac || echo DEFAULTcase似乎没有返回任何代码。

From bash manual :bash 手册

If the ';;'如果';;' operator is used, no subsequent matches are attempted after the first pattern match.使用运算符,在第一个模式匹配后不会尝试后续匹配。 Using ';&' in place of ';;'使用 ';&' 代替 ';;' causes execution to continue with the command-list associated with the next clause, if any.使执行继续执行与下一个子句关联的命令列表(如果有)。 Using ';;&' in place of ';;'使用 ';;&' 代替 ';;' causes the shell to test the patterns in the next clause, if any, and execute any associated command-list on a successful match, continuing the case statement execution as if the pattern list had not matched.使外壳程序测试下一个子句中的模式(如果有),并在成功匹配时执行任何关联的命令列表,继续执行 case 语句,就好像模式列表不匹配一样。

Maybe such idea:也许这样的想法:

case "$action" in
a|b)
    echo for a or b
    ;;&
b|c)
    echo for c or b
    ;;&
a|b|c) ;;
*)
    echo for everything ELSE
esac

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM