case с условием when

Поток управления / Условные операторы / Операторы switch/case

shopt -s extglob

#case has no "when": the extra condition is an if inside the branch
describe() {
    case $1 in
        +([0-9]))
            if (( $1 > 1000000 )); then
                echo "Big number"
            else
                echo "Small number"
            fi
            ;;
        *)
            if (( ${#1} < 256 )); then
                echo "Short string"
            else
                echo "Long string"
            fi
            ;;
    esac
}

result1=$(describe 5000000)
#result1 is "Big number"
result2=$(describe "tiger")
#result2 is "Short string"

#;;& goes on to check the next patterns (bash 4.0 and newer),
#so one value can go into several branches
password="Secret123"
labels=()
case $password in
    *[0-9]*)
        labels+=("digits")
        ;;&
    *[A-Z]*)
        labels+=("capitals")
        ;;&
    ????????*)
        labels+=("long")
        ;;
esac
#labels is ("digits" "capitals" "long")

#;& runs the next branch without its check, like a case without break in C
level=2
steps=()
case $level in
    3)
        steps+=("three")
        ;&
    2)
        steps+=("two")
        ;&
    1)
        steps+=("one")
        ;;
esac
#steps is ("two" "one")

echo "result1 is $result1"
echo "result2 is $result2"
echo "labels is (${labels[*]})"
echo "steps is (${steps[*]})"