Case 选择语句,主要用于对多个选择条件进行匹配输出,与 if elif 语句结构类似,通常用于脚本传递输入参数,打印出输出结果及内容,其语法格式以 Case…in 开头,esac结尾。
语法格式如下:
#!/bin/bash
#By author jfedu.net 2017
case $1 in
Pattern1)
语句 1
;;
Pattern2)
语句 2
;;
Pattern3)
语句 3
;;
esac
Case 条件语句 Shell 脚本编程案例如下:
(1) 打印 Monitor 及 Archive 选择菜单:
#!/bin/bash
#By author jfedu.net 2017
case $1 in
monitor)
monitor_log
;;
archive)
archive_log
;;
help )
echo –e "\033[32mUsage:{$0 monitor | archive |help }\033[0m"
;;
*)
echo –e "\033[32mUsage:{$0 monitor | archive |help }\033[0m "
esac
(2) 自动修改 IP 脚本菜单:
#!/bin/bash
#By author jfedu.net 2017
case $i in
modify_ip)
change_ip
;;
modify_hosts)
change_hosts
;;
exit)
exit
;;
*)
echo -e "1) modify_ip\n2) modify_ip\n3)exit"
esac