当前位置:网站首页>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
边栏推荐
- V79.01 Hongmeng kernel source code analysis (user mode locking) | how to use the fast lock futex (Part 1) | hundreds of blogs analyze the openharmony source code
- 用C语言编程:用公式计算:e≈1+1/1!+1/2! …+1/n!,精度为10-6
- 个人装修笔记
- Yolov3, 4, 5 and 6 Summary of target detection
- Nacos - 配置管理
- 软件工程师面试刷题网站、经验方法
- Audio audiorecord create (I)
- NFT监管要点和海外政策
- Introduction to R language
- DataBinding源码分析
猜你喜欢

Intelligent water and fertilizer integrated control system
![[MFC development (17)] advanced list control list control](/img/e8/24c52cb51defc6c96b43c2ef3232ff.png)
[MFC development (17)] advanced list control list control

如何做好固定资产管理?易点易动提供智能化方案

C语言指针的进阶(下)

19Mn6 German standard pressure vessel steel plate 19Mn6 Wugang fixed binding 19Mn6 chemical composition

It is designed with high bandwidth, which is almost processed into an open circuit?

Foundation: 2 The essence of image

Brief introduction to AES

MATLAB小技巧(16)矩阵特征向量特征值求解一致性验证--层次分析

What is the material of 15CrMoR, mechanical properties and chemical analysis of 15CrMoR
随机推荐
Shell脚本-echo命令 转义符
3、Modbus通讯协议详解
Matlab tips (16) consistency verification of matrix eigenvector eigenvalue solution -- analytic hierarchy process
基础:2.图像的本质
如何解决固定资产管理和盘点的难题?
I would like to know the process of stock registration and account opening by mobile phone? In addition, is it safe to open a mobile account?
Glitch Free时钟切换技术
Matlab tips (23) matrix analysis -- simulated annealing
Nacos - 服务发现
【MFC开发(16)】树形控件Tree Control
【js逆向】md5加密参数破解
Intelligent water and fertilizer integrated control system
个人装修笔记
挖财打新股安全吗
【无标题】
《单片机原理及应用》-片外拓展
win7 pyinstaller打包exe 后报错 DLL load failed while importing _socket:参数错误
"Analysis of 43 cases of MATLAB neural network": Chapter 30 design of combined classifier based on random forest idea - breast cancer diagnosis
软件工程师面试刷题网站、经验方法
The use of word in graduation thesis