Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Case statement fallthrough?

Tags:

bash

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:
;&'

like image 239
Resorath Avatar asked Aug 17 '12 17:08

Resorath


People also ask

What is Fallthrough in switch case?

Code Inspection: Fallthrough in 'switch' statementReports a switch statement where control can proceed from a branch to the next one. Such "fall-through" often indicates an error, for example, a missing break or return .

What is Fallthrough statement?

Fallthrough in C++ Fall through is a type of error that occurs in various programming languages like C, C++, Java, Dart …etc. It occurs in switch-case statements where when we forget to add a break statement and in that case flow of control jumps to the next line.

Where is [[ Fallthrough ]] used?

A fallthrough statement may only be used in a switch statement, where the next statement to be executed is a statement with a case or default label for that switch statement.

What is Fallthrough in switch statement in Java?

Fall through condition: This condition occurs in the switch control statement when there is no break keyword mention for the particular case in the switch statement and cause execution of the cases till no break statement occurs or exit from the switch statement.


Video Answer


2 Answers

The ;& and ;;& operators were introduced in bash 4.0, so if you want to stick with a five year old version of bash, you'll either have to repeat code, or use ifs.

if (( a == 1)); then echo quick; fi if (( a > 0 && a <= 2)); then echo brown; fi  if (( a > 0 && a <= 3)); then echo fox; fi if (( a == 4)); then echo jumped; fi 

or find some other way to achieve the actual goal.

(On a side note, don't use all uppercase variable names. You risk overwriting special shell variables or environment variables.)

like image 55
geirha Avatar answered Oct 14 '22 17:10

geirha


Try this:

case $VAR in normal)     echo "This doesn't do fallthrough"     ;; fallthrough)     echo -n "This does "     ;& somethingelse)     echo "fall-through"     ;; esac 
like image 38
René Steetskamp Avatar answered Oct 14 '22 18:10

René Steetskamp