Does bash have a `default`-equivalent for the case statement when using fallthrough?
Asked Answered
M

1

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

Maryammaryann answered 10/12, 2019 at 0:45 Comment(0)
T
10

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
Tartuffery answered 10/12, 2019 at 0:51 Comment(7)
that's what I have (original pre-edit post had a typo) I need to fix the final statement, it should trigger for "everything ELSE", but is triggering for "everything"Maryammaryann
So just use ;; ... It's designed to work that way... I don't understand, then why do you use a fallthrough if you don't want it to fallthrough? Please post some sample inputs and example outputs for the inputs. Maybe make the second ;;& a ;;? That way *) will not execute if b is matched.Tartuffery
Then you lose the a|b) -> b|c), the whole point of the question. It's specifically asking for the ability to use both, which other languages I've needed it in haveMaryammaryann
I do want fallthrough, but I dont want fallthrough-*), I want default. *) is a hack because bash doesnt have default, but they're the same thing when not using fallthrough. This question is: "but what if I am using fallthrough? What's my default-equivalent?"Maryammaryann
To account for your edit; if you only change the second ;;& to ;; that makes the final one mean "not b|c", which is not the same as "everything else" id est "not a|b|c".Maryammaryann
haha, we got the same idea in the end. I'll delete my answer since you did it first (I didn't see it), but I'll only add an upvote. I'm really looking for a default-equivalentMaryammaryann
for anyone else coming in, I'll quote my deleted answer; "But I'd appreciate a way not to re-list all the other options (so if I edit in more switches, I don't need to remember to add it to the list at the end)."Maryammaryann

© 2022 - 2024 — McMap. All rights reserved.