(五)Shell case语句

版权声明:本文为博主原创文章,转载请指明地址。 https://blog.csdn.net/Mr_rsq/article/details/82179324

目录

1 case语法

case总结:

  1. case语句就相当于多分支的if语句。case语句的优势是更规范、易读。case能实现的语句if都能实现。
  2. case语句适合变量的值少,且为固定的数字或字符串集合。(1,2,3)或(start,stop,restart)
  3. 系统服务启动脚本传参的判断多用case语句。可以参考系统rpcbind、nfs、crond脚本。

系统标准脚本参考:

/etc/init.d/functions
/etc/init.d/nfs
/etc/init.d/rpcbind
/etc/init.d/httpd
/etc/rc.d/rc.sysinit

/etc/init.d/functions脚本详解传送门

语句小结:运维层面

  1. case主要是写启动脚本,范围较窄。
  2. if取值判断、比较,应用更广。
case "字符串变量" in1)
      指令1...
      ;;
  值1)
      指令1...
      ;;
  *)
      指令3...
esac

2 实例

2.1 范例1

用case语句实现当用户输入颜色和内容,这个内容就会被加上这种颜色,如red RSQ,那么RSQ就会被标记红色输出。
扩展:给字符加颜色
字颜色:30-37

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”

字背景颜色范围:40-47

echo -e “\033[40;37m 黑底白字 \033[0m” 
echo -e “\033[41;37m 红底白字 \033[0m” 
echo -e “\033[42;37m 绿底白字 \033[0m” 
echo -e “\033[43;37m 黄底白字 \033[0m” 
echo -e “\033[44;37m 蓝底白字 \033[0m” 
echo -e “\033[45;37m 紫底白字 \033[0m” 
echo -e “\033[46;37m 天蓝底白字 \033[0m” 
echo -e “\033[47;30m 白底黑字 \033[0m”

最后控制选项:

\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 显示光标

颜色测试

[root@RSQ ~]# cat >>/server/scripts/color.sh<<EOF
#!/bin/bash
RED_COLOR='\E[;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 "$RED_COLOR RSQ $RES"
echo -e "$GREEN_COLOR RSQ $RES"
echo -e "$YELLOW_COLOR RSQ $RES"
echo -e "$BLUE_COLOR RSQ $RES"
echo -e "$PINK_COLOR RSQ $RES"
EOF

这里写图片描述

【解答】

[root@RSQ scripts]# cat color.sh 
#!/bin/bash
RED_COLOR='\E[;31m'
GREEN_COLOR='\E[1;32m'
YELLOW_COLOR='\E[1;33m'
BLUE_COLOR='\E[1;34m'
PINK_COLOR='\E[1;35m'
RES='\E[0m'

usage() {
    echo "USAGE: $0 [red|RED|green|GREEN|yellow|YELLOW|blue|BLUE|pink|PINK] content"
    exit 1
}

color() {
    case "$1" in
      red|RED)
          echo -e "${RED_COLOR}$2${RES}"
          ;;
      green|GREEN)
          echo -e "${GREEN_COLOR}$2${RES}"
          ;;
      yellow|YELLOW)
          echo -e "${YELLOW_COLOR}$2${RES}"
          ;;
      blue|BLUE)
          echo -e "${BLUE_COLOR}$2${RES}"
          ;;
      pink|PINK)
          echo -e "${PINK_COLOR}$2{$RES}"
          ;;
      *)
          usage
    esac
}

main() {
    if [ $# -ne 2 ]; then
        usage
    fi
    color $1 $2
}

main $*

这里写图片描述

2.2 范例2

执行脚本打印一个水果菜单,当用户选择水果的时候,打印告诉它选择的水果是什么,并给水果加上一种颜色,用case语句实现,最好能无限循环除了手动退出。菜单效果如下:

1.apple
2.pear
3.banana
4.cherry
5.exit

【解答】

[root@RSQ scripts]# cat case.sh 
#!/bin/bash

RED_COLOR='\E[;31m'
YELLOW_COLOR='\E[1;33m'
SHAN='\E[31;5m'
RES='\E[0m'

menu() {
  cat <<END
    1.apple
    2.pear
    3.banana
    4.cherry
    5.exit
END
  read -p "Pls input your choice:" fruit
}

usage() {
    # 下边echo内容会以红色闪烁显示
    echo -e "${SHAN}USAGE: Pls input the following options :${RES}"   
    echo "====================================================="
}

color() {
    case "$fruit" in 
      1)
          echo -e "The color of ${RED_COLOR}apple${RES} is RED."
          ;;
      2)
          echo -e "The color of ${YELLOW_COLOR}pear${RES} is YELLOW."
          ;;
      3)
          echo -e "The color of ${YELLOW_COLOR}banana${RES} is YELLOW."
          ;;
      4)
          echo -e "The color of ${RED_COLOR}cherry${RES} is RED."
          ;;
      5)
          exit 0
          ;;
      *)
          usage
    esac
}

main() {
    while true
    do
        menu
        color
    done
}

main $*

这里写图片描述

2.3 范例3

已知nginx管理命令为:

启动:/application/nginx/sbin/nginx
停止:/application/nginx/sbin/nginx -s stop
重新加载:/application/nginx/sbin/nginx -s reload
请用case脚本模拟nginx服务启动关闭,并实现可通过chkconfig管理:
/etc/init.d/nginx {start|stop|restart|reload}

【解答】

[root@RSQ scripts]# cat /etc/init.d/nginx 
#!/bin/bash
# chkconfig: 2345 30 62
# description: Nginx [start|stop|restart|reload]

[ -f /etc/init.d/functions ] && . /etc/init.d/functions
pid_file=/application/nginx/logs/nginx.pid
nginx=/application/nginx/sbin/nginx
RETVAL=0

usage() {
    echo "USAGE: $0 [start|stop|restart|reload]"
    exit 1
}

judge() {
        RETVAL=$?
        if [ $RETVAL -eq 0 ]; then
            action "$1 nginx" /bin/true
        else
            action "$1 nginx" /bin/false
        fi
}

start() {
    if [ -f $pid_file ]; then
        echo "nginx is running......"
    else
        $nginx
        judge start
    fi
    return $RETVAL
}

stop() {
    if [ -f $pid_file ]; then
        $nginx -s stop
        judge stop
    else 
        echo "nginx is stopped"
    fi
    return $RETVAL
}
reload() {
    if [ -f $pid_file ]; then
        $nginx -s reload
        judge reload
    else
        echo "Cannot reload nginx service ,no such $pid_file files."
    fi
    return $RETVAL
}

main() {
    case "$1" in
      start)
          start
          ;;
      stop)
          stop
          ;;
      restart)
          stop
          sleep 2
          start
          ;;
      reload)
          reload
          ;;
      *)
          usage
    esac
}

main $*
[root@RSQ scripts]# chmod +x /etc/init.d/nginx

猜你喜欢

转载自blog.csdn.net/Mr_rsq/article/details/82179324