In popular imperative languages, switch statements generally "fall through" to the next level once a case statement has been matched.
Example:
int a = 2;
switch(a)
{
case 1:
print "quick ";
case 2:
print "brown ";
case 3:
print "fox ";
break;
case 4:
print "jumped ";
}
would print "brown fox".
However the same code in bash
A=2
case $A in
2)
echo "QUICK"
;&
2)
echo "BROWN"
;&
3)
echo "FOX"
;&
4)
echo "JUMPED"
;&
esac
only prints "BROWN"
How do I make the case statement in bash "fall through" to the remaining conditions like the first example?
(edit: Bash version 3.2.25, the ;& statement (from wiki) results in a syntax error)
running:
test.sh:
#!/bin/bash
A=2
case $A in
1)
echo "QUICK"
;&
2)
echo "BROWN"
;&
3)
echo "FOX"
;&
esac
Gives:
./test.sh: line 6: syntax error near unexpected token ;' ./test.sh:
line 6:
;&'
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…