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变量值,进入程序匹配比较
- )
- echo "the num you input is 1"
- ;;
- )
- echo "the num you input is 2"
- ;;
- [-])
- 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:
- the num you input is
- [root@web1 scripts]# ./test26.sh
- please input a number:
- the num you input is
- [root@web1 scripts]# ./test26.sh
- please input a number:
- the num you input is
- [root@web1 scripts]# ./test26.sh
- please input a number:
- please input [-] int
- [root@web1 scripts]#
2)if表达式方法
- [root@web1 scripts]# cat test27.sh
- #!/bin/bash
- read -p "please input a number:" ans
- if [ $ans -eq ];then
- echo "the num you input is 1"
- elif [ $ans -eq ];then
- echo "the num you input is 2"
- elif [ $ans -eq -a $ans -le ];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-.sh test22.sh test24.sh test26.sh test2.sh
- [root@web1 scripts]# ./test27.sh
- please input a number:
- the num you input is
- [root@web1 scripts]# ./test27.sh
- please input a number:
- the num you input must be [-]
- [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更好
- =====================
- .apple
- .pear
- .banana
- .cherry
- =====================
- '
- read -p "pls select a num:" num #提示用户输入
- case "$num" in
- ) #如果是1就输出echo的命令,下了一次类推
- echo -e "${RED_COLOR}apple${RES}"
- ;;
- )
- echo -e "${GREEN_COLOR}pear${RES}"
- ;;
- )
- echo -e "${YELLOW_COLOR}banana${RES}"
- ;;
- )
- echo -e "${BLUE_COLOR}cherry${RES}"
- ;;
- *)
- echo "muse be {1|2|3|4}" #如果不匹配就提示
#这里省略了双分号- esac
- [root@web1 scripts]# ./test29.sh
- =====================
- .apple
- .pear
- .banana
- .cherry
- =====================
- pls select a num:
- apple
- [root@web1 scripts]# ./test29.sh
- =====================
- .apple
- .pear
- .banana
- .cherry
- =====================
- pls select a num:
- pear
- [root@web1 scripts]# ./test29.sh
- =====================
- .apple
- .pear
- .banana
- .cherry
- =====================
- pls select a num:
- 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
- )
- echo -e "${RED_COLOR}apple${RES}"
- ;;
- )
- echo -e "${GREEN_COLOR}pear${RES}"
- ;;
- )
- echo -e "${YELLOW_COLOR}banana${RES}"
- ;;
- esac
- [root@web1 scripts]# ./test30.sh
- .apple
- .pear
- .banana
- pls input your choice:
- apple
- [root@web1 scripts]# ./test30.sh
- .apple
- .pear
- .banana
- pls input your choice:
- 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
- }
- function menu(){ #菜单内容写成函数,也是方便重复使用
- cat <<END
- .apple
- .pear
- .banana
- END
- }
- function chose(){ #将输入也写成函数,方便重复使用
- read -p "pls input your choice:" fruit
- case "$fruit" in
- )
- echo -e "${RED_COLOR}apple${RES}"
- ;;
- )
- echo -e "${GREEN_COLOR}pear${RES}"
- ;;
- )
- echo -e "${YELLOW_COLOR}banana${RES}"
- ;;
- *)
- usage
- esac
- }
- function main(){ #主函数,执行定义的所有函数,这是程序的入口,模拟c的编程方式
- menu
- chose
- }
- main #执行主函数,如下:
- [root@web1 scripts]# ./test31.sh
- .apple
- .pear
- .banana
- pls input your choice:
- apple
- [root@web1 scripts]# ./test31.sh
- .apple
- .pear
- .banana
- pls input your choice:
- USAGE: ./test31.sh {|||}
- [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 ];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 ];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
shell 学习笔记8-case条件语句的更多相关文章
- Go语言学习笔记五: 条件语句
Go语言学习笔记五: 条件语句 if语句 if 布尔表达式 { /* 在布尔表达式为 true 时执行 */ } 竟然没有括号,和python很像.但是有大括号,与python又不一样. 例子: pa ...
- shell脚本中的case条件语句介绍和使用案例
#前言:这篇我们接着写shell的另外一个条件语句case,上篇讲解了if条件语句.case条件语句我们常用于实现系统服务启动脚本等场景,case条件语句也相当于if条件语句多分支结构,多个选择,ca ...
- Java学习笔记之——if条件语句和三目运算符
一.if条件语句 语法: if(条件){ 执行语句1: }else{ 执行语句2: } 二.三目运算符 ? 条件运算符 语法: 表达式?结果1:结果2: 如果表达式结果为true,则结果为结果1 ...
- shell 学习笔记9-while/until循环语句
一.while循环语句 1.循环语句 循环愈久就是重复执行一条指令或一组执行,知道条件不在满足时停止,shell循环语句包括,while.until.for.select语句 2.while循环 主要 ...
- SHELL学习笔记----IF条件判断,判断条件
SHELL学习笔记----IF条件判断,判断条件 前言: 无论什么编程语言都离不开条件判断.SHELL也不例外. if list then do something here ...
- 【python学习笔记】5.条件、循环和其他语句
[python学习笔记]5.条件.循环和其他语句 print: 用来打印表达式,不管是字符串还是其他类型,都输出以字符串输出:可以通过逗号分隔输出多个表达式 import: 导入模块 impo ...
- 『忘了再学』Shell流程控制 — 35、多分支case条件语句
目录 1.case条件语句介绍 2.case语句需要注意的内容 3.练习 示例1 示例2 1.case条件语句介绍 case语句和if-elif-else语句一样都是多分支条件语句,不过和if多分支条 ...
- shell学习笔记
shell学习笔记 .查看/etc/shells,看看有几个可用的Shell . 曾经用过的命令存在.bash_history中,但是~/.bash_history记录的是前一次登录前记录的所有指令, ...
- [转帖][Bash Shell] Shell学习笔记
[Bash Shell] Shell学习笔记 http://www.cnblogs.com/maybe2030/p/5022595.html 阅读目录 编译型语言 解释型语言 5.1 作为可执行程序 ...
随机推荐
- 【转】45个实用的JavaScript技巧、窍门和最佳实践
原文:https://colobu.com/2014/09/23/45-Useful-JavaScript-Tips,-Tricks-and-Best-Practices/ 目录 [−] 列表 第一次 ...
- 【转】暴力破解无线WiFi密码
# coding:utf-8 import pywifi from pywifi import const import time from asyncio.tasks import sleep cl ...
- Tekla 导出ifc并浏览
Tekla导出IFC
- Python3入门(十三)——连接数据库
以Mysql为例: 要操作关系数据库,首先需要连接到数据库,一个数据库连接称为Connection: 连接到数据库后,需要打开游标,称之为Cursor,通过Cursor执行SQL语句,然后,获得执行结 ...
- .gitignore 模板
.gitignore 模板 HELP.md target/ !.mvn/wrapper/maven-wrapper.jar !**/src/main/** !**/src/test/** ### ST ...
- java使用ffmpeg生成HLS切片文件
/*** * 将文件切割成片 * @param filename * @param uuid * @param data * @throws IOException */ default void d ...
- Spring Boot系列之-helloword入门
一. What: Spring Boot是什么?以1.4.3.RELEASE为例,官方介绍为:http://docs.spring.io/spring-boot/docs/1.4.3.RELEASE/ ...
- 关于Js异常
一.Javascript的异常处理机制 当javascript代码中出现错误的时候,js引擎就会根据js的调用栈逐级寻找对应的catch,如果没有找到相应的catch handler或catch ha ...
- PHP mbstring通过多字节字符串扩展处理中文查找、计算问题
最近有个需求有到了mbstring相关的函数进行中文处理,如下: mb_strpos mb_strlen 过程中遇到一点比较奇怪的问题,及在本地环境运行没有问题 但我们生产环境是2台服务器,其中一台正 ...
- canal启动报错ERROR c.a.o.canal.parse.inbound.mysql.dbsync.DirectLogFetcher - I/O error while reading from client socket