0%

Linux中的Switch Case

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

语法如下:

case EXPRESSION in

PATTERN_1)
STATEMENTS
;;

PATTERN_2)
STATEMENTS
;;

PATTERN_N)
STATEMENTS
;;

*)
STATEMENTS
;;
esac

case里还可以有一些语法:

?() - 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,匹配指定模式外的情况

举例说明:

# 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

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

case "$ENV" in
*DEV ) xxx;;
*QA|*UAT ) yyy;;
PROD ) zzz;;
* ) xxx;;
esac

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

ENV=$( tr '[:upper:]' '[:lower:]' <<<"$ENV" )
欢迎到微信里去当吃瓜群众