Switch case with fallthrough?

前端 未结 5 920
一生所求
一生所求 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:32

    If the values are integer then you can use [2-3] or you can use [5,7,8] for non continuous values.

    #!/bin/bash
    while [ $# -gt 0 ];
    do
        case $1 in
        1)
            echo "one"
            ;;
        [2-3])
            echo "two or three"
            ;;
        [4-6])
            echo "four to six"
            ;;
        [7,9])
            echo "seven or nine"
            ;;
        *)
            echo "others"
            ;;
        esac
        shift
    done
    

    If the values are string then you can use |.

    #!/bin/bash
    while [ $# -gt 0 ];
    do
        case $1 in
        "one")
            echo "one"
            ;;
        "two" | "three")
            echo "two or three"
            ;;
        *)
            echo "others"
            ;;
        esac
        shift
    done
    

提交回复
热议问题