case结构条件句
语法:
case "字符串变量" in
值1) 指令 1...
;;
值2) 指令 2...
;;
*) 指令 3...
esac
提示:case语句相当于一个if的多分支结构语句。
case语句范例:
1.根据用户输入判断是哪个数字(case-1.sh)
如果用户输入1或2 或3,则输出对应的数字,如果是其他内容,返回不正确,退出。
[root@localhost ~]# cat case-1.sh
#!/bin/sh
case $1 in
1)
echo "You input number is $1"
;;
2)
echo "You input number is $1"
;;
3)
echo "You input number is $1"
;;
*)
echo "You input number is Error!,must be less 3"
esac
[root@localhost ~]# sh case-1.sh 2
You input number is 2
[root@localhost ~]# sh case-1.sh 4
You input number is Error!
实例1:
执行脚本打印一个水果菜单如下:
1.apple
2.pear
3.banana
4.cherry
当用户选择水果的时候,打印告诉他选择的水果是什么,并给水果单词加上一种颜色,用case语句实现。
方法一:
#!/bin/sh
read -p "
please choose one fruit for under list:
1.apple
2.pear
3.banana
4.cherry
Plesae Input You Choose:" num
ChangColor(){
RED_COLOR=' \E[1;31m'
GREEN_COLOR=' \E[1;32m'
YELLOW_COLOR=' \E[1;33m'
BLUE_COLOR=' \E[1;34m'
PINK=' \E[1;35m'
RES=' \E[0m'
case $num in
1) echo -e "${RED_COLOR}apple $RES"
;;
2) echo -e "${GREEN_COLOR}pear $RES"
;;
3) echo -e "${YELLOW_COLOR}banana $RES"
;;
4) echo -e "${PINK}cherry $RES"
;;
*) echo -e "You input number is $num,must be gt 0 and less 4"
esac
}
ChangColor $num
方法二:(menu函数打印菜单,其他一样)
#!/bin/sh
menu(){
cat <<END
please choose one fruit for under list:
1.apple
2.pear
3.banana
4.cherry
END
}
menu
read -p "Plesae Input You Choose:" num
ChangColor(){
RED_COLOR=' \E[1;31m'
GREEN_COLOR=' \E[1;32m'
YELLOW_COLOR=' \E[1;33m'
BLUE_COLOR=' \E[1;34m'
PINK=' \E[1;35m'
RES=' \E[0m'
case $num in
1) echo -e "${RED_COLOR}apple $RES"
;;
2) echo -e "${GREEN_COLOR}pear $RES"
;;
3) echo -e "${YELLOW_COLOR}banana $RES"
;;
4) echo -e "${PINK}cherry $RES"
;;
*) echo -e "You input number is $num,must be gt 0 and less 4"
esac
}
ChangColor $num
打印菜单方式三:
cat <<<EOF
please choose one fruit for under list:
1.apple
2.pear
3.banana
4.cherry
EOF
打印菜单方式四:
echo '
please choose one fruit for under list:
1.apple
2.pear
3.banana
4.cherry
'
拓展:让echo输出字符串显示不同颜色
字体颜色范围:30——37
语法:
echo -e "\033[30m 黑色字体 testsize \033[0m";
echo -e "\033[31m 红色字体 testsize \033[0m";
echo -e "\033[32m 绿色字体 testsize \033[0m";
echo -e "\033[33m 黄色字体 testsize \033[0m";
echo -e "\033[34m 蓝色字体 testsize \033[0m";
echo -e "\033[35m 紫色字体 testsize \033[0m";
echo -e "\033[36m 天蓝色字体 testsize \033[0m";
echo -e "\033[37m 白色字体 testsize \033[0m";
执行结果:
字体颜色范围:30——37
语法:
echo -e "\033[40;37m 黑底白字 testsize \033[0m";
echo -e "\033[41;37m 红底白字 testsize \033[0m";
echo -e "\033[42;37m 绿底白字 testsize \033[0m";
echo -e "\033[43;37m 黄底白字 testsize \033[0m";
echo -e "\033[44;37m 蓝底白字 testsize \033[0m";
echo -e "\033[45;37m 紫底白字 testsize \033[0m";
echo -e "\033[46;37m 天蓝底白字 testsize \033[0m";
echo -e "\033[47;30m 白底黑字 testsize \033[0m";
执行结果:
控制选项说明:
==ANSI 控制码的说明
\33[0m 关闭所有属性
\33[1m 设置高亮度
\33[4m 下划线
\33[5m 闪烁
\33[7m 反显
\33[8m 消隐
\33[30m -- \33[37m 设置前景色
\33[40m -- \33[47m 设置背景色
\33[nA 光标上移n行
\33[nB 光标下移n行
\33[nC 光标右移n行
\33[nD 光标左移n行
\33[y;xH设置光标位置
\33[2J 清屏
\33[K 清除从光标到行尾的内容
\33[s 保存光标位置
\33[u 恢复光标位置
\33[?25l 隐藏光标
\33[?25h 显示光标
有关文字及背景颜色可以参考:man console_codes
实例1:请用case脚本模拟nginx服务启动关闭:
Nginx管理命令为:
启动:/application/nginx/sbin/nginx
停止:/application/nginx/sbin/nginx -s stop
重新加载:/application/nginx/sbin/nginx -s reload
输出选择菜单:/etc/init.d/nginx {start|stop|restart|reload}
并实现可通过chkconfig管理。
实例2:编写一个mysql单实例或多实例启动脚本
mysql启动命令:mysql_safe --defaults-file=/data/3306/my.cnf &
mysql停止命令:mysqladmin -u root -p123456 -S /data/3306/mysql.sock shutdown
要求用函数、if、case等实现