一、case语句简介

  1、什么是case条件语句

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

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

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

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

  2、语法

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

  3、逻辑图

二、范例

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

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

    1)case方式表达

  1. [root@web1 scripts]# cat test26.sh
  2. #!/bin/bash
  3. #this script is created by zxg
  4. #www.cnblogs.com/zhangxingeng/
  5. read -p "please input a number:" ans        #<---打印信息提示用户输入,输入信息复制给ans变量
  6. case "$ans" in                      #<---case语句获取ans变量值,进入程序匹配比较
  7. )
  8. echo "the num you input is 1"
  9. ;;
  10. )
  11. echo "the num you input is 2"
  12. ;;
  13. [-])
  14. echo "the num you input is $ans"
  15. ;;
  16. *)
  17. echo "please input [0-9] int"
  18. exit;                    #<---esac语句结束最后一个值,可以省略双分号
  19. esac  
  20. [root@web1 scripts]# ./test26.sh
  21. please input a number:
  22. the num you input is
  23. [root@web1 scripts]# ./test26.sh
  24. please input a number:
  25. the num you input is
  26. [root@web1 scripts]# ./test26.sh
  27. please input a number:
  28. the num you input is
  29. [root@web1 scripts]# ./test26.sh
  30. please input a number:
  31. please input [-] int
  32. [root@web1 scripts]#

    2)if表达式方法

  1. [root@web1 scripts]# cat test27.sh
  2. #!/bin/bash
  3. read -p "please input a number:" ans
  4. if [ $ans -eq ];then
  5. echo "the num you input is 1"
  6. elif [ $ans -eq ];then
  7. echo "the num you input is 2"
  8. elif [ $ans -eq -a $ans -le ];then
  9. echo "the num you input is $ans"
  10. else
  11. echo "the num you input must be [1-9]"
  12. exit
  13. fi
  14.  
  15. [root@web1 scripts]# ./test2
  16. test20.sh test21.sh test23.sh test25.sh test27.sh
  17. test21-.sh test22.sh test24.sh test26.sh test2.sh
  18. [root@web1 scripts]# ./test27.sh
  19. please input a number:
  20. the num you input is
  21. [root@web1 scripts]# ./test27.sh
  22. please input a number:
  23. the num you input must be [-]
  24. [root@web1 scripts]#

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

    (1)apple

    (2)pear

    (3)banana

    (4)cherry

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

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

  1. [root@web1 scripts]# cat test28.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. echo -e "$RED_COLOR zxg $RES"
  9. echo -e "$YELLOW_COLOR zxg $RES"
  10. [root@web1 scripts]#

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

  1. [root@web1 scripts]# chmod +x test29.sh
  2. [root@web1 scripts]# cat test29.sh
  3. #!/bin/bash
  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. RES='\E[0m'
  9. echo '                #echo打印菜单,可以是使用cat更好
  10. =====================
  11. .apple
  12. .pear
  13. .banana
  14. .cherry
  15. =====================
  16. '
  17. read -p "pls select a num:" num  #提示用户输入
  18. case "$num" in
  19. )                  #如果是1就输出echo的命令,下了一次类推
  20. echo -e "${RED_COLOR}apple${RES}"    
  21. ;;
  22. )
  23. echo -e "${GREEN_COLOR}pear${RES}"
  24. ;;
  25. )
  26. echo -e "${YELLOW_COLOR}banana${RES}"
  27. ;;
  28. )
  29. echo -e "${BLUE_COLOR}cherry${RES}"
  30. ;;
  31. *)
  32. echo "muse be {1|2|3|4}"        #如果不匹配就提示
                              #这里省略了双分号
  33. esac
  34.  
  35. [root@web1 scripts]# ./test29.sh
  36.  
  37. =====================
  38. .apple
  39. .pear
  40. .banana
  41. .cherry
  42. =====================
  43.  
  44. pls select a num:
  45. apple
  46. [root@web1 scripts]# ./test29.sh
  47.  
  48. =====================
  49. .apple
  50. .pear
  51. .banana
  52. .cherry
  53. =====================
  54.  
  55. pls select a num:
  56. pear
  57. [root@web1 scripts]# ./test29.sh
  58.  
  59. =====================
  60. .apple
  61. .pear
  62. .banana
  63. .cherry
  64. =====================
  65.  
  66. pls select a num:
  67. banana
  68. [root@web1 scripts]# ./test29.sh

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

  1. [root@web1 scripts]# chmod +x test30.sh
  2. [root@web1 scripts]# cat test30.sh
  3. #!/bin/bash
  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. RES='\E[0m'
  9. menu(){
  10. cat <<END          #另外一种方式是cat也可以用select循环,不常用,还有函数的方式也可以使用,方便重复使用
  11. 1.apple
  12. 2.pear
  13. 3.banana
  14. END
  15. }
  16. menu
  17. read -p "pls input your choice:" fruit
  18. case "$fruit" in
  19. )
  20. echo -e "${RED_COLOR}apple${RES}"
  21. ;;
  22. )
  23. echo -e "${GREEN_COLOR}pear${RES}"
  24. ;;
  25. )
  26. echo -e "${YELLOW_COLOR}banana${RES}"
  27. ;;
  28. esac
  29. [root@web1 scripts]# ./test30.sh
  30. .apple
  31. .pear
  32. .banana
  33. pls input your choice:
  34. apple
  35. [root@web1 scripts]# ./test30.sh
  36. .apple
  37. .pear
  38. .banana
  39. pls input your choice:
  40. no fruit you choose.
  41. [root@web1 scripts]#

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

  [root@web1 scripts]# cat test31.sh

  1. #!/bin/bash
  2. RED_COLOR='\E[1;31m'
  3. GREEN_COLOR='\E[1;32m'
  4. YELLOW_COLOR='\E[1;33m'
  5. BLUE_COLOR='\E[1;34m'
  6. RES='\E[0m'
  7. function usage(){                #使用帮助写成usage函数,方便重复使用
  8. echo "USAGE: $0 {1|2|3|4}"
  9. exit
  10. }
  11. function menu(){                #菜单内容写成函数,也是方便重复使用
  12. cat <<END
  13. .apple
  14. .pear
  15. .banana
  16. END
  17. }
  18. function chose(){                  #将输入也写成函数,方便重复使用
  19. read -p "pls input your choice:" fruit
  20. case "$fruit" in
  21. )
  22. echo -e "${RED_COLOR}apple${RES}"
  23. ;;
  24. )
  25. echo -e "${GREEN_COLOR}pear${RES}"
  26. ;;
  27. )
  28. echo -e "${YELLOW_COLOR}banana${RES}"
  29. ;;
  30. *)
  31. usage
  32. esac
  33. }
  34. function main(){                  #主函数,执行定义的所有函数,这是程序的入口,模拟c的编程方式
  35. menu
  36. chose                        
  37. }
  38. main                        #执行主函数,如下:                                  
  39. [root@web1 scripts]# ./test31.sh
  40. .apple
  41. .pear
  42. .banana
  43. pls input your choice:
  44. apple
  45. [root@web1 scripts]# ./test31.sh
  46. .apple
  47. .pear
  48. .banana
  49. pls input your choice:
  50. USAGE: ./test31.sh {|||}
  51. [root@web1 scripts]#

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

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

    1)内容加上不同的颜色

  1. [root@web1 scripts]# echo -e "\033[30m 黑色 zxg \033[0m"
  2. 黑色 zxg
  3. [root@web1 scripts]# echo -e "\033[31m 红色 zxg \033[0m"
  4. 红色 zxg
  5. [root@web1 scripts]# echo -e "\033[32m 绿色 zxg \033[0m"
  6. 绿色 zxg
  7. [root@web1 scripts]# echo -e "\033[33m 棕色 zxg \033[0m"
  8. 棕色 zxg
  9. [root@web1 scripts]# echo -e "\033[34m 蓝绿色 zxg \033[0m"
  10. 蓝绿色 zxg
  11. [root@web1 scripts]# echo -e "\033[35m 洋红色 zxg \033[0m"
  12. 洋红色 zxg
  13. [root@web1 scripts]# echo -e "\033[37m 洋红色 zxg \033[0m"
  14. 洋红色 zxg

      详细颜色,可以man console_codes

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

  1. [root@web1 scripts]# cat test32.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. PINK='\E[1;35m'
  8. RES='\E[0m'                          #ANSI控制码,关闭属性
  9. echo -e "${RED_COLOR}======red color======${RES}"
  10. echo -e "${YELLOW_COLOR}======yellow color======${RES}"
  11. echo -e "${BLUE_COLOR}======blue color======${RES}"
  12. echo -e "${GREEN_COLOR}======green color======${RES}"
  13. echo -e "${PINK}======pink color======${RES}"
  14. [root@web1 scripts]#

      效果

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

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

  1. [root@web1 scripts]# cat test33.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. PINK='\E[1;35m'
  8. RES='\E[0m'
  9. if [ $# -ne ];then                      #用if语句打印帮助提示,如果传入的参数个数不等于2,则打印
  10. echo "Usage $0 content {red|yellow|blue|green}"
  11. exit
  12. fi
  13. case "$2" in                          #$2是脚本传入的第二个参数,颜色字符串
  14. red|RED)
  15. echo -e "${RED_COLOR}$1${RES}"         #给第一个参数$1加颜色
  16. ;;
  17. yellow|YELLOW)
  18. echo -e "${YELLOW_COLOR}$1${RES}"
  19. ;;
  20. green|GREEN)
  21. echo -e "${GREEN_COLOR}$1${RES}"
  22. ;;
  23. blue|BLUE)
  24. echo -e "${BLUE_COLOR}$1${RES}"
  25. ;;
  26. pink|PINK)
  27. echo -e "${PINK_COLOR}$1${RES}"
  28. ;;
  29. *)
  30. echo "Usage $0 content {red|yellow|blue|green}"
  31. exit
  32. esac
  33.  
  34. [root@web1 scripts]# ./test33.sh
  35. Usage ./test33.sh content {red|yellow|blue|green}

      效果

    另外一种更专业的写法

  1. #!/bin/bash
  2. function AddColor(){                #定义加颜色的函数AddColor
  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. PINK='\E[1;35m'
  8. RES='\E[0m'
  9. if [ $# -ne 2 ];then
  10. echo "Usage $0 content {red|yellow|blue|green}"
  11. exit
  12. fi
  13. case "$2" in
  14. red|RED)
  15. echo -e "${RED_COLOR}$1${RES}"
  16. ;;
  17. yellow|YELLOW)
  18. echo -e "${YELLOW_COLOR}$1${RES}"
  19. ;;
  20. green|GREEN)
  21. echo -e "${GREEN_COLOR}$1${RES}"
  22. ;;
  23. blue|BLUE)
  24. echo -e "${BLUE_COLOR}$1${RES}"
  25. ;;
  26. pink|PINK)
  27. echo -e "${PINK_COLOR}$1${RES}"
  28. ;;
  29. *)
  30. echo "Usage $0 content {red|yellow|blue|green}"
  31. exit
  32. esac
  33. }
  34. function main(){                    #定义主函数main
  35. AddColor $1 $2                   #令颜色函数后面接$1和$2,即将函数参数转换为获取脚本的参数,注意,这里的$1和$2不能用引号括起来,否则脚本不传参也会被AddColor认为已传入参数
  36. }
  37. main $*                         #执行main函数,利用$*接受命令行的所有参数,并传入到main函数

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

[root@web1 scripts]# cat test34.sh

  1. #!/bin/bash
    plus_color(){                    #定义指定字符内容加颜色的函数,就是上面的例子包上定义为函数
  2. RED_COLOR='\E[1;31m'
  3. GREEN_COLOR='\E[1;32m'
  4. YELLOW_COLOR='\E[1;33m'
  5. BLUE_COLOR='\E[1;34m'
  6. PINK='\E[1;35m'
  7. RES='\E[0m'
  8. if [ $# -ne ];then
  9. echo "Usage $0 content {red|yellow|blue|green}"
  10. exit
  11. fi
  12. case "$2" in
  13. red|RED)
  14. echo -e "${RED_COLOR}$1${RES}"
  15. ;;
  16. yellow|YELLOW)
  17. echo -e "${YELLOW_COLOR}$1${RES}"
  18. ;;
  19. green|GREEN)
  20. echo -e "${GREEN_COLOR}$1${RES}"
  21. ;;
  22. blue|BLUE)
  23. echo -e "${BLUE_COLOR}$1${RES}"
  24. ;;
  25. pink|PINK)
  26. echo -e "${PINK_COLOR}$1${RES}"
  27. ;;
  28. *)
  29. echo "Usage $0 content {red|yellow|blue|green}"
  30. exit
  31. esac
  32. }
  33. plus_color "i" red                      #传入I字符和红色单词给plus_color函数
  34. plus_color "am" green                    #传入am字符和绿色单词给Plus_color函数
  35. plus_color "zxg" blue                    #传入zxg字符和颜色单词给plus_color函数

      运行结果

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

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

  1. [root@web1 scripts]# echo -e "\033[40;37m 黑底白字zxg\033[0m"
  2. 黑底白字zxg
  3. [root@web1 scripts]# echo -e "\033[41;37m 红底白字zxg\033[0m"
  4. 红底白字zxg
  5. [root@web1 scripts]# echo -e "\033[42;37m 绿底白字zxg\033[0m"
  6. 绿底白字zxg
  7. [root@web1 scripts]# echo -e "\033[43;37m 棕底白字zxg\033[0m"
  8. 棕底白字zxg
  9. [root@web1 scripts]# echo -e "\033[43;37m 绿底白字zxg\033[0m"
  10. 绿底白字zxg
  11. [root@web1 scripts]# echo -e "\033[44;37m 绿底白字zxg\033[0m"
  12. 绿底白字zxg
  13. [root@web1 scripts]# echo -e "\033[40;37m 黑底白字zxg\033[0m"
  14. 黑底白字zxg
  15. [root@web1 scripts]# echo -e "\033[41;37m 红底白字zxg\033[0m"
  16. 红底白字zxg
  17. [root@web1 scripts]# echo -e "\033[42;37m 绿底白字zxg\033[0m"
  18. 绿底白字zxg
  19. [root@web1 scripts]# echo -e "\033[43;37m 棕底白字zxg\033[0m"
  20. 棕底白字zxg
  21. [root@web1 scripts]# echo -e "\033[44;37m 蓝底白字zxg\033[0m"
  22. 蓝底白字zxg
  23. [root@web1 scripts]# echo -e "\033[45;37m 洋红底白字zxg\033[0m"
  24. 洋红底白字zxg
  25. [root@web1 scripts]# echo -e "\033[46;37m 蓝绿底白字zxg\033[0m"
  26. 蓝绿底白字zxg
  27. [root@web1 scripts]# echo -e "\033[47;30m 白底黑字zxg\033[0m"
  28. 白底黑字zxg
  29. [root@web1 scripts]#

    执行效果

   

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

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

    

    

shell 学习笔记8-case条件语句的更多相关文章

  1. Go语言学习笔记五: 条件语句

    Go语言学习笔记五: 条件语句 if语句 if 布尔表达式 { /* 在布尔表达式为 true 时执行 */ } 竟然没有括号,和python很像.但是有大括号,与python又不一样. 例子: pa ...

  2. shell脚本中的case条件语句介绍和使用案例

    #前言:这篇我们接着写shell的另外一个条件语句case,上篇讲解了if条件语句.case条件语句我们常用于实现系统服务启动脚本等场景,case条件语句也相当于if条件语句多分支结构,多个选择,ca ...

  3. Java学习笔记之——if条件语句和三目运算符

    一.if条件语句 语法: if(条件){ 执行语句1: }else{ 执行语句2: } 二.三目运算符 ?    条件运算符 语法: 表达式?结果1:结果2: 如果表达式结果为true,则结果为结果1 ...

  4. shell 学习笔记9-while/until循环语句

    一.while循环语句 1.循环语句 循环愈久就是重复执行一条指令或一组执行,知道条件不在满足时停止,shell循环语句包括,while.until.for.select语句 2.while循环 主要 ...

  5. SHELL学习笔记----IF条件判断,判断条件

    SHELL学习笔记----IF条件判断,判断条件 前言: 无论什么编程语言都离不开条件判断.SHELL也不例外.  if list then           do something here   ...

  6. 【python学习笔记】5.条件、循环和其他语句

    [python学习笔记]5.条件.循环和其他语句 print: 用来打印表达式,不管是字符串还是其他类型,都输出以字符串输出:可以通过逗号分隔输出多个表达式 import: 导入模块     impo ...

  7. 『忘了再学』Shell流程控制 — 35、多分支case条件语句

    目录 1.case条件语句介绍 2.case语句需要注意的内容 3.练习 示例1 示例2 1.case条件语句介绍 case语句和if-elif-else语句一样都是多分支条件语句,不过和if多分支条 ...

  8. shell学习笔记

    shell学习笔记 .查看/etc/shells,看看有几个可用的Shell . 曾经用过的命令存在.bash_history中,但是~/.bash_history记录的是前一次登录前记录的所有指令, ...

  9. [转帖][Bash Shell] Shell学习笔记

    [Bash Shell] Shell学习笔记 http://www.cnblogs.com/maybe2030/p/5022595.html  阅读目录 编译型语言 解释型语言 5.1 作为可执行程序 ...

随机推荐

  1. 【转】45个实用的JavaScript技巧、窍门和最佳实践

    原文:https://colobu.com/2014/09/23/45-Useful-JavaScript-Tips,-Tricks-and-Best-Practices/ 目录 [−] 列表 第一次 ...

  2. 【转】暴力破解无线WiFi密码

    # coding:utf-8 import pywifi from pywifi import const import time from asyncio.tasks import sleep cl ...

  3. Tekla 导出ifc并浏览

    Tekla导出IFC

  4. Python3入门(十三)——连接数据库

    以Mysql为例: 要操作关系数据库,首先需要连接到数据库,一个数据库连接称为Connection: 连接到数据库后,需要打开游标,称之为Cursor,通过Cursor执行SQL语句,然后,获得执行结 ...

  5. .gitignore 模板

    .gitignore 模板 HELP.md target/ !.mvn/wrapper/maven-wrapper.jar !**/src/main/** !**/src/test/** ### ST ...

  6. java使用ffmpeg生成HLS切片文件

    /*** * 将文件切割成片 * @param filename * @param uuid * @param data * @throws IOException */ default void d ...

  7. Spring Boot系列之-helloword入门

    一. What: Spring Boot是什么?以1.4.3.RELEASE为例,官方介绍为:http://docs.spring.io/spring-boot/docs/1.4.3.RELEASE/ ...

  8. 关于Js异常

    一.Javascript的异常处理机制 当javascript代码中出现错误的时候,js引擎就会根据js的调用栈逐级寻找对应的catch,如果没有找到相应的catch handler或catch ha ...

  9. PHP mbstring通过多字节字符串扩展处理中文查找、计算问题

    最近有个需求有到了mbstring相关的函数进行中文处理,如下: mb_strpos mb_strlen 过程中遇到一点比较奇怪的问题,及在本地环境运行没有问题 但我们生产环境是2台服务器,其中一台正 ...

  10. canal启动报错ERROR c.a.o.canal.parse.inbound.mysql.dbsync.DirectLogFetcher - I/O error while reading from client socket