How to use patterns in a case statement?

后端 未结 3 1632
天涯浪人
天涯浪人 2020-12-04 09:52

The man page says that case statements use \"filename expansion pattern matching\".
I usually want to have short names for some parameters, so

3条回答
  •  既然无缘
    2020-12-04 10:19

    Brace expansion doesn't work, but *, ? and [] do. If you set shopt -s extglob then you can also use extended pattern matching:

    • ?() - zero or one occurrences of pattern
    • *() - zero or more occurrences of pattern
    • +() - one or more occurrences of pattern
    • @() - one occurrence of pattern
    • !() - anything except the pattern

    Here's an example:

    shopt -s extglob
    for arg in apple be cd meet o mississippi
    do
        # call functions based on arguments
        case "$arg" in
            a*             ) foo;;    # matches anything starting with "a"
            b?             ) bar;;    # matches any two-character string starting with "b"
            c[de]          ) baz;;    # matches "cd" or "ce"
            me?(e)t        ) qux;;    # matches "met" or "meet"
            @(a|e|i|o|u)   ) fuzz;;   # matches one vowel
            m+(iss)?(ippi) ) fizz;;   # matches "miss" or "mississippi" or others
            *              ) bazinga;; # catchall, matches anything not matched above
        esac
    done
    

提交回复
热议问题