当前位置:网站首页>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
边栏推荐
- 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?
- 機動目標跟踪——當前統計模型(CS模型)擴展卡爾曼濾波/無迹卡爾曼濾波 matlab實現
- 明明设计的是高带宽,差点加工成开路?
- Computer tips
- Suivi des cibles de manoeuvre - - mise en oeuvre du modèle statistique actuel (modèle CS) filtre Kalman étendu / filtre Kalman sans trace par MATLAB
- 我想知道手机注册股票开户的流程?另外,手机开户安全么?
- vscode自定义各个区域的颜色
- Bimianhongfu queren()
- Intelligent water and fertilizer integrated control system
- Shell脚本-位置参数(命令行参数)
猜你喜欢

个人装修笔记

Nacos - 配置管理

DID的使用指南,原理

AES简单介绍

C语言指针的进阶(上篇)

动态代理

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

What is the material of 15CrMoR, mechanical properties and chemical analysis of 15CrMoR

Mavros sends a custom topic message to Px4

Agrometeorological environment monitoring system
随机推荐
如何一站式高效管理固定资产?
如何做好固定资产管理?易点易动提供智能化方案
How to use OKR as the leadership framework of marketing department
[Yu Yue education] Shandong Vocational College talking about railway reference materials
【MFC开发(17)】高级列表控件List Control
It is designed with high bandwidth, which is almost processed into an open circuit?
Matlab tips (16) consistency verification of matrix eigenvector eigenvalue solution -- analytic hierarchy process
How can enterprises and developers take the lead in the outbreak of cloud native landing?
Centos7 shell script one click installation of JDK, Mongo, Kafka, FTP, PostgreSQL, PostGIS, pgrouting
There are many problems in sewage treatment, and the automatic control system of pump station is solved in this way
The use of word in graduation thesis
VSYNC+三重缓存机制+Choreographer
Principle and application of single chip microcomputer - principle of parallel IO port
Intelligent water conservancy solution
FreeRTOS学习简易笔记
Only in China! Alicloud container service enters the Forrester leader quadrant
Mavros sends a custom topic message to Px4
Redis源码学习(29),压缩列表学习,ziplist.c(二)
Nacos - 配置管理
【C】 Summary of wrong questions in winter vacation