[关闭]
@yanglt7 2018-10-28T09:37:29.000000Z 字数 4266 阅读 611

《Shell 编程》08_case 条件语句

Shell


8.1 case 条件语句的语法

  1. case "变量" in
  2. 1)
  3. 指令 1...
  4. ;;
  5. 2)
  6. 指令 2...
  7. ;;
  8. *)
  9. 指令 3
  10. esac

8.2 case 条件语句实践

例 8-1 执行 Shell 脚本,打印一个如下的水果菜单:
(1)apple
(2)pear
(3)banana
(4)cherry

当用户输入数字选择水果时,告知选择的水果并给水果单词加上一种颜色。

给字体加颜色的命令为:

  1. [root@web001 ~]# echo -e "\E[1;31m red color ylt\E[0m"
  2. red color ylt #<== 打印的字为红色
  3. [root@web001 ~]# echo -e "\033[1;31m red color ylt\033[0m"
  4. red color ylt

在上述命令中:

有关 ANSI 控制码说明如下:

开发脚本:

  1. [root@web001 scripts]# cat fruit.sh
  2. #!/bin/bash
  3. RED_COLOR='\E[1;31m'
  4. GREEN_COLOR='\E[1;32m'
  5. YELLOW_COLOR='\E[1;33m'
  6. BLUE_COLOR='\E[1;34m'
  7. RES='\E[0m'
  8. function usage(){
  9. echo "USAGE: $0 {1|2|3|4}"
  10. exit 1
  11. }
  12. function menu(){
  13. cat <<END
  14. 1. apple
  15. 2. pear
  16. 3. banana
  17. END
  18. }
  19. function chose(){
  20. read -p "pls input your choice:" fruit
  21. case "$fruit" in
  22. 1)
  23. echo -e "${RED_COLOR}apple${RES}"
  24. ;;
  25. 2)
  26. echo -e "${GREEN_COLOR}pear${RES}"
  27. ;;
  28. 3)
  29. echo -e "${BLUE_COLOR}banana${RES}"
  30. ;;
  31. *)
  32. usage
  33. esac
  34. }
  35. function main(){
  36. menu
  37. chose
  38. }
  39. main

例 8-2 开发给指定内容加指定颜色的脚本

  1. [root@web001 ~]# echo -e "033[1;30m black color\033[0m"
  2. [root@web001 ~]# echo -e "\033[1;31m red color\033[0m"
  3. [root@web001 ~]# echo -e "\033[1;32m green color\033[0m"
  4. [root@web001 ~]# echo -e "\033[1;33m brown color\033[0m"
  5. [root@web001 ~]# echo -e "\033[1;34m blue color\033[0m"
  6. [root@web001 ~]# echo -e "\033[1;35m magenta color\033[0m"
  7. [root@web001 ~]# echo -e "\033[1;36m cyan color\033[0m"
  8. [root@web001 ~]# echo -e "\033[1;37m white color\033[0m"
  1. [root@web001 scripts]# cat color.sh
  2. #!/bin/bash
  3. function Addcolor(){
  4. RED_COLOR='\E[1;31m'
  5. GREEN_COLOR='\E[1;32m'
  6. YELLOW_COLOR='\E[1;33m'
  7. BLUE_COLOR='\E[1;34m'
  8. PINK_COLOR='\E[1;35m'
  9. RES='\E[0m'
  10. if [ $# -ne 2 ]; then
  11. echo "Usage: $0 content {red|yellow|blue|pink}"
  12. exit
  13. fi
  14. case "$2" in
  15. red|RED)
  16. echo -e "${RED_COLOR}$1${RES}"
  17. ;;
  18. green|GREEN)
  19. echo -e "${GREEN_COLOR}$1${RES}"
  20. ;;
  21. yellow|YELLOW)
  22. echo -e "${YELLOW_COLOR}$1${RES}"
  23. ;;
  24. blue|BLUE)
  25. echo -e "${BLUE_COLOR}$1${RES}"
  26. ;;
  27. pink|PINK)
  28. echo -e "${PINK_COLOR}$1${RES}"
  29. ;;
  30. *)
  31. echo "Usage: $0 content {red|yellow|blue|pink}"
  32. exit
  33. esac
  34. }
  35. function main(){
  36. Addcolor $1 $2
  37. }
  38. main $*

例 8-3 给输出的字符串加背景颜色

  1. echo -e "\033[40;37m 黑底白字 ylt\033[0m"
  2. echo -e "\033[41;37m 红底白字 ylt\033[0m"
  3. echo -e "\033[42;37m 绿底白字 ylt\033[0m"
  4. echo -e "\033[43;37m 棕底白字 ylt\033[0m"
  5. echo -e "\033[44;37m 蓝底白字 ylt\033[0m"
  6. echo -e "\033[45;37m 洋红底白字 ylt\033[0m"
  7. echo -e "\033[46;37m 蓝绿底白字 ylt\033[0m"
  8. echo -e "\033[47;30m 白底黑字 ylt\033[0m"

例 8-4 实现通过传参的方式往 /etc/openvpn_authfile.conf 添加用户:

1)命令用法为:

  1. USAGE: sh adduser {-add|-del|-search} username

2)如果有同名的用户,则不能添加,如果没有对应用户,则无需删除,查找到用户或没有用户时应给出明确的提示。

3)/etc/openvpn_authfile.conf 不能被所有外部用户直接删除及修改。

  1. [root@web001 scripts]# cat auth.sh
  2. #!/bin/bash
  3. #Source function library.
  4. . /etc/init.d/functions
  5. #config file path
  6. FILE_PATH=/etc/openvpn_authfile.conf
  7. [ ! -f $FILE_PATH ] && touch $FILE_PATH
  8. usage(){
  9. cat <<EOF
  10. USAGE: `basename $0` {-add|-del|-search} username
  11. EOF
  12. }
  13. #judge run user
  14. if [ $UID -ne 0 ]; then
  15. echo "You are not supper user,pls call root!"
  16. exit 1;
  17. fi
  18. #judge arg numbers
  19. if [ $# -ne 2 ]; then
  20. usage
  21. exit 2
  22. fi
  23. case "$1" in
  24. -a|-add)
  25. shift #<== remove $1,$2-->$1
  26. if grep "^$1$" ${FILE_PATH} >/dev/null 2&>1; then
  27. action $"vpnuser,$1 is exist." /bin/false
  28. exit
  29. else
  30. chattr -i ${FILE_PATH} #<==lock file
  31. /bin/cp ${FILE_PATH} ${FILE_PATH}.$(date +%F%T)
  32. echo "$1" >> ${FILE_PATH}
  33. [ $? -eq 0 ] && action $"Add $1" /bin/true
  34. chattr -i ${FILE_PATH}
  35. fi
  36. ;;
  37. -d|del)
  38. shift
  39. if [ `grep "\b$1\b" ${FILE_PATH}|wc -l` -lt 1 ]; then
  40. action $"vpnuser,$1 is not exist," /bin/false
  41. exit
  42. else
  43. chattr -i ${FILE_PATH}
  44. /bin/cp ${FILE_PATH} ${FILE_PATH}.$(date +%F%T)
  45. sed -i "/^${1}$/d" ${FILE_PATH}
  46. [ $? -eq 0 ] && action $"Del $1" /bin/true
  47. chattr -i ${FILE_PATH}
  48. exit
  49. fi
  50. ;;
  51. -s|-search)
  52. shift
  53. if [ `grep -w "$1" ${FILE_PATH}|wc -l` -lt 1 ]; then
  54. echo $"vpuser,$1 is not exist."; exit
  55. else
  56. echo $"vpnuser,$1 is exist."; exit
  57. fi
  58. ;;
  59. *)
  60. usage
  61. exit
  62. ;;
  63. esac

例 8-5 实现 Nginx 服务的启动和关闭的功能

  1. [root@web001 scripts]# cat nginx.sh
  2. #!bin/bash
  3. path=/application/nginx/sbin
  4. pid=/application/nginx/logs/nginx.pid
  5. RETVAL=0
  6. . /etc/init.d/functions
  7. start(){
  8. if [ ! -f $pid ];then
  9. $path/nginx
  10. RETVAL=$?
  11. if [ $RETVAL -eq 0 ]; then
  12. action "nginx is started." /bin/true
  13. return $RETVAL
  14. fi
  15. else
  16. echo "nginx is running"
  17. return 0
  18. fi
  19. }
  20. stop(){
  21. if [ -f $pid ]; then
  22. $path/nginx -s stop
  23. RETVAL=$?
  24. if [ $RETVAL -eq 0 ]; then
  25. action "nginx is stopped." /bin/true
  26. return $RETVAL
  27. else
  28. action "nginx is stopped." /bin/false
  29. return $RETVAL
  30. fi
  31. else
  32. echo "nginx is not running"
  33. return $RETVAL
  34. fi
  35. }
  36. case "$1" in
  37. start)
  38. start
  39. RETVAL=$?
  40. ;;
  41. stop)
  42. stop
  43. RETVAL=$?
  44. ;;
  45. restart)
  46. stop
  47. sleep 1
  48. start
  49. RETVAl=$?
  50. ;;
  51. *)
  52. echo $"Usage: $0 {start|stop|restart}"
  53. exit 1
  54. esac
  55. exit $RETVAL
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注