当前位置:网站首页>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
边栏推荐
- MAVROS发送自定义话题消息给PX4
- R语言入门
- 《单片机原理与应用》——并行IO口原理
- 为什么LTD独立站就是Web3.0网站!
- Nacos - gestion de la configuration
- 我想知道手机注册股票开户的流程?另外,手机开户安全么?
- Qt的模型与视图
- [detailed explanation of Huawei machine test] judgment string subsequence [2022 Q1 Q2 | 200 points]
- Do you know how data is stored? (C integer and floating point)
- Advanced C language pointer (Part 2)
猜你喜欢
![Matlab [function derivation]](/img/ba/9fb9da8a458d0c74b29b21a17328fc.png)
Matlab [function derivation]

Internet of things technology is widely used to promote intelligent water automation management

What is the material of 16mo3 steel plate? What is the difference between 16mo3 and Q345R?
![[MFC development (16)] tree control](/img/b9/1de4330c0bd186cfe062b02478c058.png)
[MFC development (16)] tree control

Dynamic proxy

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

1.jetson与摄像头的对接

Advanced level of C language pointer (Part 1)
![[detailed explanation of Huawei machine test] judgment string subsequence [2022 Q1 Q2 | 200 points]](/img/0f/972cde8c749e7b53159c9d9975c9f5.png)
[detailed explanation of Huawei machine test] judgment string subsequence [2022 Q1 Q2 | 200 points]

MATLAB【函数求导】
随机推荐
电脑小技巧
AES简单介绍
Nacos - Configuration Management
Glitch Free时钟切换技术
性能提升2-3倍!百度智能云第二代昆仑芯服务器上线
3. Detailed explanation of Modbus communication protocol
基础:3.opencv快速入门图像和视频
日常办公耗材管理解决方案
R语言观察日志(part24)--初始化设置
Introduction to R language
Configuration and startup of Chang'an chain synchronization node
NIO-零拷贝
2022.2.15
动态代理
Dynamic proxy
中断与其他函数共享变量、临界资源的保护
MATLAB【函数和图像】
C语言指针的进阶(上篇)
Programming with C language: calculate with formula: e ≈ 1+1/1+ 1/2! …+ 1/n!, Accuracy is 10-6
Shell脚本-for循环和for int循环