当前位置:网站首页>Shell脚本-case in 和正则表达式
Shell脚本-case in 和正则表达式
2022-07-01 08:36:00 【小蜗牛的路】
case in 的 pattern 部分支持简单的正则表达式,具体来说,可以使用以下几种格式:
格式 | 说明 |
---|---|
* | 表示任意字符串。 |
[abc] | 表示 a、b、c 三个字符中的任意一个。比如,[15ZH] 表示 1、5、Z、H 四个字符中的任意一个。 |
[m-n] | 表示从 m 到 n 的任意一个字符。比如,[0-9] 表示任意一个数字,[0-9a-zA-Z] 表示字母或数字。 |
| | 表示多重选择,类似逻辑运算中的或运算。比如,abc | xyz 表示匹配字符串 “abc” 或者 “xyz”。 |
如果不加以说明,Shell 的值都是字符串,expression 和 pattern 也是按照字符串的方式来匹配的;本节第一段代码看起来是判断数字是否相等,其实是判断字符串是否相等。
最后一个分支*)
并不是什么语法规定,它只是一个正则表达式,*
表示任意字符串,所以不管 expression 的值是什么,*)
总能匹配成功。
代码
#!/bin/bash
printf "Input a character: "
read -n 1 char
case $char in
[a-zA-Z])
printf "\nletter\n"
;;
[0-9])
printf "\nDigit\n"
;;
[0-9])
printf "\nDigit\n"
;;
[,.?!])
printf "\nPunctuation\n"
;;
*)
printf "\nerror\n"
esac
输出:
运行结果1:
Input integer number: S
letter
运行结果2:
Input integer number: ,
Punctuation
边栏推荐
- 《单片机原理及应用》-片外拓展
- VSYNC+三重缓存机制+Choreographer
- Vscode customize the color of each area
- Nacos - 配置管理
- [Yu Yue education] Shandong Vocational College talking about railway reference materials
- MATLAB【函数和图像】
- 一文纵览主流 NFT 市场平台版税、服务费设计
- Tita OKR: a dashboard to master the big picture
- What is the material of 16MnDR, the minimum service temperature of 16MnDR, and the chemical composition of 16MnDR
- Nacos - gestion de la configuration
猜你喜欢
随机推荐
分享2022上半年我读过的7本书
R语言观察日志(part24)--初始化设置
软件工程师面试刷题网站、经验方法
Agrometeorological environment monitoring system
In depth learning training sample amplification and tag name modification
内存大小端
Shell脚本-select in循环
Shell脚本-for循环和for int循环
3. Detailed explanation of Modbus communication protocol
长安链同步节点配置与启动
C language student information management system
Nacos - 配置管理
为什么LTD独立站就是Web3.0网站!
MATLAB小技巧(16)矩阵特征向量特征值求解一致性验证--层次分析
Advanced API
Matlab [function derivation]
Yolov3, 4, 5 and 6 Summary of target detection
《微机原理》——微处理器内部及外部结构
Shell脚本-if else语句
Model and view of QT