Switch case with fallthrough?

前端 未结 5 905
一生所求
一生所求 2020-12-04 07:43

I am looking for the correct syntax of the switch statement with fallthrough cases in Bash (ideally case-insensitive). In PHP I would program it like:

switch         


        
5条回答
  •  一向
    一向 (楼主)
    2020-12-04 08:21

    Recent bash versions allow fall-through by using ;& in stead of ;;: they also allow resuming the case checks by using ;;& there.

    for n in 4 14 24 34
    do
      echo -n "$n = "
      case "$n" in
       3? )
         echo -n thirty-
         ;;&   #resume (to find ?4 later )
       "24" )
         echo -n twenty-
         ;&   #fallthru
       "4" | [13]4)
         echo -n four 
         ;;&  # resume ( to find teen where needed )
       "14" )
         echo -n teen
      esac
      echo 
    done
    

    sample output

    4 = four
    14 = fourteen
    24 = twenty-four
    34 = thirty-four
    

提交回复
热议问题