shell 学习笔记8-case条件语句

一、case语句简介

  1、什么是case条件语句

    case条件语句就相当于多分支的if/elif/else条件语句,但是比这样的语句更规范更好看,经常被用在失效系统服务启动脚本等企业应用中

    程序将case获取的变量的值与表达式部分的值1、值2、值3等逐个进行比较,如果获取的变量值和某个值相匹配,就会执行值后面对应的执行知道执行双分号;;才停止,然后在跳出case语句主体,执行case语句(esac字符)后面的其他命令

    如果没找到匹配变量的任何值,则执行"*)"后面的指令,通常是给使用者的使用提示直到遇到双分号;;或者esac结束,这个类似if多分支语句中最后else语句

    case语句表达式对应值的部分,可以使用管道等更多功能来匹配

  2、语法

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

  3、逻辑图

二、范例

  1、根据用户输入判断用户收入的是哪个数字

    如果用户输入的是1~9的任易数字,则输出对应输入的数字;如果是其他数字级字符,则发回输入不正确的提示,并退出

    1)case方式表达

[root@web1 scripts]# cat test26.sh 
#!/bin/bash
#this script is created by zxg
#www.cnblogs.com/zhangxingeng/
read -p "please input a number:" ans        #<---打印信息提示用户输入,输入信息复制给ans变量
case "$ans" in                      #<---case语句获取ans变量值,进入程序匹配比较
        1)
                echo "the num you input is 1"
                ;;
        2)
                echo "the num you input is 2"
                ;;
        [3-9])
                echo "the num you input is $ans"
                ;;
        *)
                echo "please input [0-9] int"
                exit;                    #<---esac语句结束最后一个值,可以省略双分号
esac  
[root@web1 scripts]# ./test26.sh 
please input a number:1
the num you input is 1
[root@web1 scripts]# ./test26.sh 
please input a number:3
the num you input is 3
[root@web1 scripts]# ./test26.sh 
please input a number:7
the num you input is 7
[root@web1 scripts]# ./test26.sh 
please input a number:10
please input [0-9] int
[root@web1 scripts]# 

     2)if表达式方法

[root@web1 scripts]# cat test27.sh 
#!/bin/bash
read -p "please input a number:" ans
if [ $ans -eq 1 ];then
        echo "the num you input is 1"
elif [ $ans -eq 2 ];then
        echo "the num you input is 2"
elif [ $ans -eq 3 -a $ans -le 9 ];then
        echo "the num you input is $ans"
else
        echo "the num you input must be [1-9]"
exit
fi

[root@web1 scripts]# ./test2
test20.sh    test21.sh    test23.sh    test25.sh    test27.sh    
test21-1.sh  test22.sh    test24.sh    test26.sh    test2.sh     
[root@web1 scripts]# ./test27.sh 
please input a number:1
the num you input is 1
[root@web1 scripts]# ./test27.sh 
please input a number:10
the num you input must be [1-9]
[root@web1 scripts]# 

  2、执行shell脚本,打印一个如下的水果菜单

    (1)apple

    (2)pear

    (3)banana

    (4)cherry

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

    1)首先集中常用的颜色,并把zxg的字符串加上颜色

[root@web1 scripts]# cat test28.sh 
#!/bin/bash
RED_COLOR='\E[1;31m'
GREEN_COLOR='\E[1;32m'
YELLOW_COLOR='\E[1;33m'
BLUE_COLOR='\E[1;34m'
RES='\E[0m'
echo -e "$RED_COLOR zxg $RES"
echo -e "$YELLOW_COLOR zxg $RES"
[root@web1 scripts]# 

    2)然后开始做带颜色的水果菜单

[root@web1 scripts]# chmod +x test29.sh 
[root@web1 scripts]# cat test29.sh 
#!/bin/bash
RED_COLOR='\E[1;31m'
GREEN_COLOR='\E[1;32m'
YELLOW_COLOR='\E[1;33m'
BLUE_COLOR='\E[1;34m'
RES='\E[0m'
echo '                #echo打印菜单,可以是使用cat更好
  =====================
  1.apple
  2.pear
  3.banana
  4.cherry
  =====================
'
read -p "pls select a num:" num  #提示用户输入
case "$num" in
    1)                  #如果是1就输出echo的命令,下了一次类推
      echo -e "${RED_COLOR}apple${RES}"    
      ;; 
    2)
      echo -e "${GREEN_COLOR}pear${RES}"
      ;;
    3)
      echo -e "${YELLOW_COLOR}banana${RES}"
      ;;
    4)
      echo -e "${BLUE_COLOR}cherry${RES}"
      ;;
    *)
      echo "muse be {1|2|3|4}"        #如果不匹配就提示
                          #这里省略了双分号
esac [root@web1 scripts]# ./test29.sh ===================== 1.apple 2.pear 3.banana 4.cherry ===================== pls select a num:1 apple [root@web1 scripts]# ./test29.sh ===================== 1.apple 2.pear 3.banana 4.cherry ===================== pls select a num:2 pear [root@web1 scripts]# ./test29.sh ===================== 1.apple 2.pear 3.banana 4.cherry ===================== pls select a num:3 banana [root@web1 scripts]# ./test29.sh

    3)cat打印菜单的方法,推荐

[root@web1 scripts]# chmod +x test30.sh
[root@web1 scripts]# cat test30.sh 
#!/bin/bash
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          #另外一种方式是cat也可以用select循环,不常用,还有函数的方式也可以使用,方便重复使用
    1.apple
    2.pear
    3.banana
END
}
menu
read -p "pls 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}" 
      ;;
esac
[root@web1 scripts]# ./test30.sh 
    1.apple
    2.pear
    3.banana
pls input your choice:1
apple
[root@web1 scripts]# ./test30.sh 
    1.apple
    2.pear
    3.banana
pls input your choice:5
no fruit you choose.
[root@web1 scripts]# 

    4)函数的方式表达,更加的专业

  [root@web1 scripts]# cat test31.sh

#!/bin/bash
RED_COLOR='\E[1;31m'
GREEN_COLOR='\E[1;32m'
YELLOW_COLOR='\E[1;33m'
BLUE_COLOR='\E[1;34m'
RES='\E[0m'
function usage(){                #使用帮助写成usage函数,方便重复使用
    echo "USAGE: $0 {1|2|3|4}"
    exit 1
}
function menu(){                #菜单内容写成函数,也是方便重复使用
    cat <<END
    1.apple
    2.pear
    3.banana
END
}
function chose(){                  #将输入也写成函数,方便重复使用
    read -p "pls 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}" 
          ;;
        *)
          usage
    esac
}
function main(){                  #主函数,执行定义的所有函数,这是程序的入口,模拟c的编程方式
menu chose                         } main                        #执行主函数,如下:                                   [root@web1 scripts]# .
/test31.sh 1.apple 2.pear 3.banana pls input your choice:1 apple [root@web1 scripts]# ./test31.sh 1.apple 2.pear 3.banana pls input your choice:5 USAGE: ./test31.sh {1|2|3|4} [root@web1 scripts]#

  3、给输出的字符串加颜色

    这个比较有趣了,通过echo 的-e参数,结合特殊的数字给不同的字符加上颜色并显示

    1)内容加上不同的颜色

[root@web1 scripts]# echo -e "\033[30m 黑色 zxg  \033[0m" 
 黑色 zxg  
[root@web1 scripts]# echo -e "\033[31m 红色 zxg  \033[0m"           
 红色 zxg  
[root@web1 scripts]# echo -e "\033[32m 绿色 zxg  \033[0m"     
 绿色 zxg  
[root@web1 scripts]# echo -e "\033[33m 棕色 zxg  \033[0m"     
 棕色 zxg  
[root@web1 scripts]# echo -e "\033[34m 蓝绿色 zxg  \033[0m"    
 蓝绿色 zxg  
[root@web1 scripts]# echo -e "\033[35m 洋红色 zxg  \033[0m"                
 洋红色 zxg  
[root@web1 scripts]# echo -e "\033[37m 洋红色 zxg  \033[0m" 
 洋红色 zxg  

      详细颜色,可以man console_codes

    2)定义变量的方式给字体加颜色

[root@web1 scripts]# cat test32.sh
#!/bin/bash
RED_COLOR='\E[1;31m'
GREEN_COLOR='\E[1;32m'
YELLOW_COLOR='\E[1;33m'
BLUE_COLOR='\E[1;34m'
PINK='\E[1;35m'
RES='\E[0m'                          #ANSI控制码,关闭属性
echo -e  "${RED_COLOR}======red color======${RES}"
echo -e  "${YELLOW_COLOR}======yellow color======${RES}"
echo -e  "${BLUE_COLOR}======blue color======${RES}"
echo -e  "${GREEN_COLOR}======green color======${RES}"
echo -e  "${PINK}======pink color======${RES}"
[root@web1 scripts]# 

      效果

 

    3)开发一个指定内容加指定颜色的脚本

        传参是,第一参数是内容,第二个参数是指定颜色

[root@web1 scripts]# cat test33.sh
#!/bin/bash
RED_COLOR='\E[1;31m'
GREEN_COLOR='\E[1;32m'
YELLOW_COLOR='\E[1;33m'
BLUE_COLOR='\E[1;34m'
PINK='\E[1;35m'
RES='\E[0m'
if [ $# -ne 2 ];then                      #用if语句打印帮助提示,如果传入的参数个数不等于2,则打印
    echo "Usage $0 content {red|yellow|blue|green}"
    exit
fi
case "$2" in                          #$2是脚本传入的第二个参数,颜色字符串
    red|RED)
         echo -e  "${RED_COLOR}$1${RES}"          #给第一个参数$1加颜色
         ;;
    yellow|YELLOW)
         echo -e  "${YELLOW_COLOR}$1${RES}"
         ;;
    green|GREEN)
         echo -e  "${GREEN_COLOR}$1${RES}"
         ;;
    blue|BLUE)
         echo -e  "${BLUE_COLOR}$1${RES}"
         ;;
    pink|PINK)
         echo -e  "${PINK_COLOR}$1${RES}"
         ;;
    *)
         echo "Usage $0 content {red|yellow|blue|green}"
         exit
esac


[root@web1 scripts]# ./test33.sh 
Usage ./test33.sh content {red|yellow|blue|green}

      效果

    另外一种更专业的写法

#!/bin/bash
function AddColor(){                #定义加颜色的函数AddColor
RED_COLOR='\E[1;31m' GREEN_COLOR='\E[1;32m' YELLOW_COLOR='\E[1;33m' BLUE_COLOR='\E[1;34m' PINK='\E[1;35m' RES='\E[0m' if [ $# -ne 2 ];then echo "Usage $0 content {red|yellow|blue|green}" exit fi case "$2" in red|RED) echo -e "${RED_COLOR}$1${RES}" ;; yellow|YELLOW) echo -e "${YELLOW_COLOR}$1${RES}" ;; green|GREEN) echo -e "${GREEN_COLOR}$1${RES}" ;; blue|BLUE) echo -e "${BLUE_COLOR}$1${RES}" ;; pink|PINK) echo -e "${PINK_COLOR}$1${RES}" ;; *) echo "Usage $0 content {red|yellow|blue|green}" exit esac } function main(){                    #定义主函数main AddColor $1 $2                   #令颜色函数后面接$1和$2,即将函数参数转换为获取脚本的参数,注意,这里的$1和$2不能用引号括起来,否则脚本不传参也会被AddColor认为已传入参数 } main $*                         #执行main函数,利用$*接受命令行的所有参数,并传入到main函数

 

    4)使用case,通过脚本传入指定内容和指定颜色

 [root@web1 scripts]# cat test34.sh

#!/bin/bash
plus_color(){                    #定义指定字符内容加颜色的函数,就是上面的例子包上定义为函数 RED_COLOR
='\E[1;31m' GREEN_COLOR='\E[1;32m' YELLOW_COLOR='\E[1;33m' BLUE_COLOR='\E[1;34m' PINK='\E[1;35m' RES='\E[0m' if [ $# -ne 2 ];then echo "Usage $0 content {red|yellow|blue|green}" exit fi case "$2" in red|RED) echo -e "${RED_COLOR}$1${RES}" ;; yellow|YELLOW) echo -e "${YELLOW_COLOR}$1${RES}" ;; green|GREEN) echo -e "${GREEN_COLOR}$1${RES}" ;; blue|BLUE) echo -e "${BLUE_COLOR}$1${RES}" ;; pink|PINK) echo -e "${PINK_COLOR}$1${RES}" ;; *) echo "Usage $0 content {red|yellow|blue|green}" exit esac } plus_color "i" red                      #传入I字符和红色单词给plus_color函数 plus_color "am" green                    #传入am字符和绿色单词给Plus_color函数
plus_color
"zxg" blue                    #传入zxg字符和颜色单词给plus_color函数

      运行结果

    5)给输出字符串加背景颜色

      字的背景颜色对应的主子范围为40~47

[root@web1 scripts]# echo -e "\033[40;37m 黑底白字zxg\033[0m" 
 黑底白字zxg
[root@web1 scripts]# echo -e "\033[41;37m 红底白字zxg\033[0m"
 红底白字zxg
[root@web1 scripts]# echo -e "\033[42;37m 绿底白字zxg\033[0m" 
 绿底白字zxg
[root@web1 scripts]# echo -e "\033[43;37m 棕底白字zxg\033[0m" 
 棕底白字zxg
[root@web1 scripts]# echo -e "\033[43;37m 绿底白字zxg\033[0m"
 绿底白字zxg
[root@web1 scripts]# echo -e "\033[44;37m 绿底白字zxg\033[0m" 
 绿底白字zxg
[root@web1 scripts]# echo -e "\033[40;37m 黑底白字zxg\033[0m"
 黑底白字zxg
[root@web1 scripts]# echo -e "\033[41;37m 红底白字zxg\033[0m"
 红底白字zxg
[root@web1 scripts]# echo -e "\033[42;37m 绿底白字zxg\033[0m"
 绿底白字zxg
[root@web1 scripts]# echo -e "\033[43;37m 棕底白字zxg\033[0m"
 棕底白字zxg
[root@web1 scripts]# echo -e "\033[44;37m 蓝底白字zxg\033[0m" 
 蓝底白字zxg
[root@web1 scripts]# echo -e "\033[45;37m 洋红底白字zxg\033[0m" 
 洋红底白字zxg
[root@web1 scripts]# echo -e "\033[46;37m 蓝绿底白字zxg\033[0m" 
 蓝绿底白字zxg
[root@web1 scripts]# echo -e "\033[47;30m 白底黑字zxg\033[0m"    
 白底黑字zxg
[root@web1 scripts]# 

    执行效果

   

转载请注明出处:https://www.cnblogs.com/zhangxingeng/p/11345169.html 

参考:https://blog.51cto.com/oldboy/1855461

    

    

posted @ 2019-08-19 18:37  乐章  阅读(740)  评论(0编辑  收藏  举报