Linux中的Switch Case
Post

Linux中的Switch Case

如果if判断超过 3 次,那么可以考虑换成switch case了。

语法如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
case EXPRESSION in

  PATTERN_1)
    STATEMENTS
    ;;

  PATTERN_2)
    STATEMENTS
    ;;

  PATTERN_N)
    STATEMENTS
    ;;

  *)
    STATEMENTS
    ;;
esac

case里还可以有一些语法:

1
2
3
4
5
?() - zero or one occurrences of pattern,匹配0次或1次
*() - zero or more occurrences of pattern,匹配0次或多次
+() - one or more occurrences of pattern,匹配1次或多次
@() - one occurrence of pattern,匹配其中的某一项
!() - anything except the pattern,匹配指定模式外的情况

举例说明:

1
2
3
4
5
6
7
8
9
10
# 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

实际上用起来不会那么高级,大概会是这样:

1
2
3
4
5
6
case "$ENV" in
    *DEV         ) xxx;;
    *QA|*UAT     ) yyy;;
    PROD         ) zzz;;
    * ) xxx;;
esac

如果要忽略大小写,就先把变量转一下再放到case里。

1
ENV=$( tr '[:upper:]' '[:lower:]' <<<"$ENV" )

Linux里的文件传输

文档站点生成工具