case小练习

echo -e "\033[30m 黑色字 \033[0m"  
echo -e "\033[31m 红色字 \033[0m"  
echo -e "\033[32m 绿色字 \033[0m"  
echo -e "\033[33m 黄色字 \033[0m"  
echo -e "\033[34m 蓝色字 \033[0m"  
echo -e "\033[35m 紫色字 \033[0m"  
echo -e "\033[36m 天蓝字 \033[0m"  
echo -e "\033[37m 白色字 \033[0m"  


1.根据水果的颜色,来打印水果,并显示对应的水果颜色

[root@shnne ~]# cat fruit.sh 
#!/bin/sh
RED_COLOR='\E[1;31m'
GREEN_COLOR='\E[1;32m'
YELLOW_COLOR='\E[1;33m'
PINK_COLOR='\E[1;35m'
RES='\E[0m'

menu(){
cat <<END
1.apple
2.pear
3.banana
4.cherry
5.quit
END
}
while true
do
echo "************"
menu
echo "************"
read -p "please input your choice:" 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)
  echo -e "$PINK_COLOR cherry $RES"
  ;;
5)
   exit 0
  ;;
*)
  echo "your choice has no fruit."
  ;;
esac
done
2.设计一个指定内容加指定颜色的脚本
[root@shnne ~]# vim getcolor.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'
if [ $# -ne 2 ] 
  then
    echo "USAGE:$0 {red|green|yellow|blue}"
    exit 1 
fi
case "$2" in
red|RED)
  echo -e "$RED_COLOR $1 $RES"
  ;;
green|GREEN)
  echo -e "$GREEN_COLOR $1 $RES"
  ;;
yellow|YELLOW)
  echo -e "$YELLOW_COLOR $1 $RES"
  ;;
blue|BLUE)
  echo -e "$BLUE_COLOR $1 $RES"
  ;;
*)
  echo "no color "
esac
分享到:
关键词:Linux运维Shell

网友留言(0 条)

发表评论