二、case结构条件句
1、语法格式
case “字符串变量” in
值1) 指令1…
;;
值2|值3|值4) 指令2…
;;
*) 指令…
esac
2、示例
(1)示例1:根据用户的选择输入,判断是哪种水果并加上不同颜色
a、给字符串加颜色
vi /services/scripts/echo-color01.sh
echo -e “\033[30m BlackFont : this is blackfont \033[0m”
echo -e “\033[31m RedFont : this is redfont \033[0m”
echo -e “\033[32m GreenFont : this is greenfont \033[0m”
echo -e “\033[33m YellowFont : this is yellowfont \033[0m”
echo -e “\033[34m BlueFont : this is bluefont \033[0m”
echo -e “\033[35m PurpleFont : this is purplefont \033[0m”
echo -e “\033[36m SkyBlueFont: this is skybluefont\033[0m”
echo -e “\033[37m WhiteFont : this is whitefont \033[0m”
vi /services/scripts/echo-color02.sh
#!/bin/sh
RED_COLOR=’\E[1;31m’
GREEN_COLOR=’\E[1;32m’
YELLOW_COLOR=’\E[1;33m’
BLUE_COLOR=’\E[1;34m’
PINK_COLOR=’\E[1;35m’
RES=’\E[0m’
echo -e “ {RES}”
echo -e “ {RES}”
echo -e “ {RES}”
echo -e “ {RES}”
echo -e “ {RES}”
b、完整脚本
vi /services/scripts/fruitmenu.sh
#!/bin/sh
RED_COLOR=’\E[1;31m’
GREEN_COLOR=’\E[1;32m’
YELLOW_COLOR=’\E[1;33m’
BLUE_COLOR=’\E[1;34m’
RES=’\E[0m’
menu(){
cat <<END
==========================
1 apple
2 pear
3 banana
4 exit
=============================
END
}
while true
do
menu
read -p "please ipunt your choice: " -t 10 fruit
case “$fruit” in
1)
echo -e"${RED_COLOR} apple ${RES}"
;;
2)
echo -e"${GREEN_COLOR} pear ${RES}"
;;
3)
echo -e"${YELLOW_COLOR} banana ${RES}"
;;
4)
exit 0
;;
*)
echo "no fruit youchoose"
;;
esac