This example is given in the linux book which I'm following to show multiple outputs if the input matches multiple cases. The book is using bash shell but I'm using zsh shell.
#! /bin/zsh
# testing the 'case' syntax of zsh
read \?"enter a character: "
case "$REPLY" in
[[:upper:]]) echo "The input is an uppercase letter.";;&
[[:lower:]]) echo "The input is a lowercase letter.";;&
[[:alpha:]]) echo "The input is an alphanumeric letter.";;&
*) echo "Input is invalid.";;&
esac
The output should be both the alphanumeric case and lowercase letter. but what the error showing is:
enter a character : f
/home/user/bin/case_testing.sh:9: parse error near `&'
Can anyone kindly provide the solution?
CodePudding user response:
In bash
, ;;&
causes the case
statement to test the next pattern instead of exiting the case
statement.
In zsh
, the corresponding terminator is ;|
.
Both bash
and zsh
use ;;
to terminate the case
statement ad ;&
to execute the list associated with the next pattern, whether or not that pattern actually matches.
The final case
statement should look like this:
case "$REPLY" in
[[:upper:]]) echo "The input is an uppercase letter." ;|
[[:lower:]]) echo "The input is a lowercase letter." ;|
[[:alnum:]]) echo "The input is an alphanumeric letter." ;;
*) echo "Input is invalid." ;;
esac
Whether or not upper
or lower
match, alnum
could match. But if alnum
succeeds, you don't want to move on to *
, so use the regular ;;
terminator.
~ % zsh tmp.sh <<< f
The input is a lowercase letter.
The input is an alphanumeric letter.
~ % zsh tmp.sh <<< F
The input is an uppercase letter.
The input is an alphanumeric letter.
~ % zsh tmp.sh <<< 3
The input is an alphanumeric letter.
~ % zsh tmp.sh <<< "#"
Input is invalid.