当前位置:网站首页>Shell脚本-case in语句
Shell脚本-case in语句
2022-07-01 08:36:00 【小蜗牛的路】
代码
#!/bin/bash
printf "Input integer number: "
read num
case $num in
1)
echo "Monday"
;;
2)
echo "Tuesday"
;;
3)
echo "Wednesday"
;;
4)
echo "Thursday"
;;
5)
echo "Friday"
;;
6)
echo "Saturday"
;;
7)
echo "Sunday"
;;
*)
echo "error"
esac
输出:
Input integer number:3
Wednesday
语法
case expression in
pattern1)
statement1
;;
pattern2)
statement2
;;
pattern3)
statement3
;;
……
*)
statementn
esac
case,in,esac是关键字,expression 表示表达式,pattern 表示匹配模式。
case 会将 expression 的值与 pattern1、pattern2、pattern3 逐个进行匹配:
- 如果 expression 和某个模式(比如 pattern2)匹配成功,就会执行这模式(比如 pattern2)后面对应的所有语句(该语句可以有一条,也可以有多条),直到遇见双分号
;;才停止;然后整个 case 语句就执行完了,程序会跳出整个 case 语句,执行esac后面的其它语句。 - 如果 expression 没有匹配到任何一个模式,那么就执行
*)后面的语句(*表示其它所有值),直到遇见双分号;;或者esac才结束。*)相当于多个 if 分支语句中最后的 else 部分。这里的;;和*)就相当于其Java中的 break 和 default。
对*)的几点说明:
- Shell case in 语句中的
*)用来“托底”,万一 expression 没有匹配到任何一个模式,*)部分可以做一些“善后”工作,或者给用户一些提示。 - 可以没有
*)部分。如果 expression 没有匹配到任何一个模式,那么就不执行任何操作。
除最后一个分支外(这个分支可以是普通分支,也可以是*)分支),其它的每个分支都必须以;;结尾,;;代表一个分支的结束,不写的话会有语法错误。最后一个分支可以写;;,也可以不写,因为无论如何,执行到 esac 都会结束整个 case in 语句。
边栏推荐
- 5mo3 UHI HII HII 17mn4 19Mn6 executive standard
- 集团公司固定资产管理的痛点和解决方案
- Computer tips
- Matlab tips (16) consistency verification of matrix eigenvector eigenvalue solution -- analytic hierarchy process
- 《单片机原理及应用》—定时器、串行通信和中断系统
- Internet of things technology is widely used to promote intelligent water automation management
- Serial port to WiFi module communication
- 中考体育项目满分标准(深圳、安徽、湖北)
- It is designed with high bandwidth, which is almost processed into an open circuit?
- Matlab [functions and images]
猜你喜欢

大型工厂设备管理痛点和解决方案

Matlab tips (16) consistency verification of matrix eigenvector eigenvalue solution -- analytic hierarchy process

Centos7 shell script one click installation of JDK, Mongo, Kafka, FTP, PostgreSQL, PostGIS, pgrouting

【面试必刷101】链表

动态代理

《MATLAB 神经网络43个案例分析》:第30章 基于随机森林思想的组合分类器设计——乳腺癌诊断

What are the differences between the architecture a, R and m of arm V7, and in which fields are they applied?

Vscode customize the color of each area
![[untitled]](/img/40/560c597d53d27eff860644cd7c3030.png)
[untitled]

如何一站式高效管理固定资产?
随机推荐
Foundation: 3 Opencv getting started images and videos
Matlab tips (16) consistency verification of matrix eigenvector eigenvalue solution -- analytic hierarchy process
Glitch free clock switching technology
大型工厂设备管理痛点和解决方案
为什么LTD独立站就是Web3.0网站!
Intelligent constant pressure irrigation system
MATLAB【函数和图像】
ARM v7的体系结构A、R、M区别,分别应用在什么领域?
你了解数据是如何存储的吗?(C整型和浮点型两类)
深度学习训练样本扩增同时修改标签名称
factory type_ Id:: create process resolution
factory type_id::create过程解析
分享2022上半年我读过的7本书
C语言学生信息管理系统
Redis源码学习(29),压缩列表学习,ziplist.c(二)
嵌入式工程师面试题3-硬件
5mo3 UHI HII HII 17mn4 19Mn6 executive standard
Nacos - gestion de la configuration
The data analyst will be ruined without project experience. These 8 project resources will not be taken away
公网集群对讲+GPS可视追踪|助力物流行业智能化管理调度