I have some bash scripts that I would like to run in OSX, but some of them contain the fallthrough case terminator ';&' which doesn't seem to work on Mac. Here's sample code:
#!/bin/bash
case $1 in
test)
echo "go" ;&
test)
echo "go2" ;;
esac
In Cygwin, .\test.sh test produces
go
go2
but on OSX,
./test.sh: line 4: syntax error near unexpected token `&'
./test.sh: line 4: ` echo "go" ;&
Since bash 4.0, a new operator ;& was introduced which provides fall through mechanism.
The case statement starts with the case keyword followed by the $variable and the in keyword. The statement ends with the case keyword backwards - esac . The script compares the input $variable against the patterns in each clause until it finds a match.
The basic syntax of the case... esac statement is to give an expression to evaluate and to execute several different statements based on the value of the expression. The interpreter checks each case against the value of the expression until a match is found. If nothing matches, a default condition will be used.
case Statement Syntax Each case statement starts with the case keyword, followed by the case expression and the in keyword. The statement ends with the esac keyword. You can use multiple patterns separated by the | operator. The ) operator terminates a pattern list.
Your code works as is on my ubuntu system, bash 4.2.24.
I think you are using an earlier version of bash which doesn't support ;&
fall-through which was added since bash 4.
From wikipedia:
Fall through is done using ;& (new since Bash 4) whereas ;; acts as a case break.
Perhaps you could try using /bin/zsh
instead of /bin/bash
? Your sample code works with “zsh 4.3.9” but not “bash 3.2.48” on my MacOS X 10.6.8.
Otherwise, the script has to be rewritten in some way… but it's hard to say exactly how without knowing what it does. Simply duplicating the code as needed is probably the simplest option and should work in every case.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With