• shell脚本之九:case结构条件句应用实践


    九、case结构条件句应用实践

    (一)case语法结构

    case结构条件句相当于多分支if条件语句,但是它比这些条件句看起来更规范工整,常被用于实现系统服务脚本等应用场景中。

    case语句的语法结构:

    case "变量" in
          值1)
              指令1
              ;;
           值2)
              指令2
              ;;
           值3)
              指令3
              ;;
            *)
              指令4
     esac
    
    

    (二)实例,case应用:

    [root@centos6-kvm3 scripts]# cat 09-03.sh
    #!/bin/bash
    cat <<EOF
    1.install lnmp
    2.install lamp
    3.exit
    EOF
    read -p "请输入一个数字{1|2|3}:" num
    expr $num + 2 &>/dev/null
    if [ $? -ne 0 ]
    then
      echo "usage:$0{1|2|3}"
      exit 1
    fi
    case $num in
         1)
         echo "install lnmp"
         ;;
         2)
         echo "install lamp"
         ;;
         3)
         echo "exit"
         exit
         ;;
         *)
         echo "usage:$0{1|2|3}"
         exit 1
    esac
    
    

    (三)实例,不同的分支字体颜色不同:

    当用户输入对应的数字选择水果的时候,告诉他选择的水果是什么,并给水果单词加上一种颜色(随意),要求用case语句实现。

    内容的颜色用数字表示,范围为30-37,每个数字代表一种颜色。
    echo -e "33[30m 黑色字oldboy trainning 33[0m" #<==30m表示黑色字。
    echo -e "33[31m 红色字oldboy trainning 33[0m" #<==31m表示红色字。
    echo -e "33[32m 绿色字oldboy trainning 33[0m" #<==32m表示绿色字。
    echo -e "33[33m 棕色字oldboy trainning 33[0m" #<==33m表示棕色字(brown),和黄色字相近。
    echo -e "33[34m 蓝色字oldboy trainning 33[0m" #<==34m表示蓝色字。
    echo -e "33[35m 洋红字oldboy trainning 33[0m" #<==35m表示洋红色字(magenta),和紫色字相近。
    echo -e "33[36m 蓝绿色oldboy trainning 33[0m" #<==36m表示蓝绿色字(cyan),和浅蓝色字相近。
    echo -e "33[37m 白色字oldboy trainning 33[0m" #<==37m表示白色字。
    

    1、基础脚本1:

    [root@centos6-kvm3 scripts]# cat 09-04.sh
    #!/bin/bash
    cat <<EOF
    1.apple
    2.pear
    3.banana
    4.cherry
    EOF
    read -p "请输入一个数字{1|2|3|4}:" num
    expr $num + 2 &>/dev/null
    if [ $? -ne 0 ]
    then
       echo "usage:$0 {1|2|3|4}"
       exit 1
    fi
    case $num in
       1)
       echo -e "33[31m apple 33[0m"
       ;;
       2)
       echo -e "33[32m pear 33[0m"
       ;;
       3)
       echo -e "33[33m banana 33[0m"
       ;;
       4)
       echo -e "33[34m cherry 33[0m"
       ;;
       *)
       echo "usage:$0 {1|2|3|4}"
       exit
    esac
    [root@centos6-kvm3 scripts]# 
    

    2、高级脚本2(带颜色):

    颜色函数:
    [root@centos6-kvm3 scripts]# cat color.sh
    #!/bin/bash
    red="33[31m"
    green="33[32m"
    yellow="33[33m"
    blue="33[34m"
    tail="33[0m"
    color(){
    case $1 in
         red)
         echo -e "${red}$2${tail}"
         ;;
         green)
         echo -e "${green}$2${tail}"
         ;;
         yellow)
         echo -e "${yellow}$2${tail}"
         ;;
         blue)
         echo -e "${blue}$2${tail}"
         ;;
         *)
         echo "usage:$0 please input right content"
    esac
    
    }
    color $*
    [root@centos6-kvm3 scripts]# 
    功能调用颜色函数:
    [root@centos6-kvm3 scripts]# cat  09-04.sh 
    #!/bin/bash
    . ./color.sh
    cat <<EOF
    1.apple
    2.pear
    3.banana
    4.cherry
    EOF
    read -p "请输入一个数字{1|2|3|4}:" num
    expr $num + 2 &>/dev/null
    if [ $? -ne 0 ]
    then
       echo "usage:$0 {1|2|3|4}"
       exit 1
    fi
    case $num in
       1)
       color red apple
       ;;
       2)
       color green pear
       ;;
       3)
       color yellow banana
       ;;
       4)
       color blue cheryy
       ;;
       *)
       echo "usage:$0 {1|2|3|4}"
       exit
    esac
    [root@centos6-kvm3 scripts]# 
    

    字体背景颜色

    字的背景颜色对应的数字范围为40-47,代码如下。
    echo -e "33[40;37m 黑底白字oldboy33[0m"   #<==40m表示黑色背景。
    echo -e "33[41;37m 红底白字oldboy33[0m"   #<==41m表示红色背景。
    echo -e "33[42;37m 绿底白字oldboy33[0m"   #<==42m表示绿色背景。
    echo -e "33[43;37m 棕底白字oldboy33[0m"   #<==43m表示棕色背景(brown),和黄色背景相近。
    echo -e "33[44;37m 蓝底白字oldboy33[0m"   #<==44m表示蓝色背景。
    echo -e "33[45;37m 洋红底白字oldboy33[0m"  #<==45m表示洋红色背景(magenta),和紫色背景相近。
    echo -e "33[46;37m蓝绿底白字oldboy33[0m"   #<==46m表示蓝绿色背景(cyan),和浅蓝色背景相近。
    echo -e "33[47;30m 白底黑字oldboy33[0m"    #<==47m表示白色背景。
    
    

    (四)rsync启动基本脚本实例:

    [root@centos6-kvm3 scripts]# cat rsync.sh
    #!/bin/bash
    case $1 in
         start)
         rsync --daemon
         if [ $? -eq 0 ]
         then
            echo "rsync $1 ok"
         else
            echo "rsync $1 fail"
         fi
         ;;
         stop)
         killall rsync
         if [ $? -eq 0 ]
         then
            echo "rsync $1 ok"
         else
            echo "rsync $1 fail"
         fi
         ;;
         restart)
         killall rsync && sleep 1 && rsync --daemon
         if [ $? -eq 0 ]
         then
            echo "rsync $1 ok"
         else
            echo "rsync $1 fail"
         fi
         ;;
         *)
         echo "usage:$0 {start|stop|restart}"
    esac
    

    查看进程:lsof -i:873

    rsync启动高级脚本:

    cp rsyncd.sh /etc/init.d/rsyncd

    chkconfig --list rsyncd

    chkconfig --add rsyncd

    chmod +x /etc/init.d/rsyncd

    (五)rsync启动脚本之高阶

    [root@centos6-kvm3 scripts]# cat rsyncd.sh 
    # chkconfig: 2345 20 80
    # description: rsync start stop
    #!/bin/bash
    . /etc/init.d/functions
    start(){
        rsync --daemon
        retval=$?
        if [ $retval -eq 0 ]
         then
            action  "rsync start ok" /bin/true
            return $retval
        else
            action "rsync start fail" /bin/false
            return $retval
        fi
    }
    stop(){
         killall rsync &>/dev/null
         retval=$?
         if [ $retval -eq 0 ]
         then
            action "rsync stop ok" /bin/true
            return $retval
         else
            action "rsync stop fail" /bin/false
            return $retval
         fi
    }
    case $1 in
         start)
         start
         retval=$?
         ;;
         stop)
         stop
         retval=$?
         ;;
         restart)
         stop 
         sleep 2
         start
         retval=$?
         ;;
         *)
         echo "usage:$0 {start|stop|restart}"
    esac
    exit $retval
    
  • 相关阅读:
    C语言I博客作业01
    C语言I博客作业09
    C语言I博客作业08
    C语言I博客作业07
    C语言I博客作业06
    C语言I博客作业05
    C语言II博客作业04
    C语言II博客作业03
    C语言II—作业02
    C语言II博客作业01
  • 原文地址:https://www.cnblogs.com/cuiyongchao007/p/12799174.html
Copyright © 2020-2023  润新知