简体   繁体   中英

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.
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.

PowerShell is able to do what I want because it has a dedicated default keyword, does Bash have something similar?
What about an exhaustive work-around like [!(a|b|c)]) or something?

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 but case doesn't seem to return any code.

From bash manual :

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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