当前位置:网站首页>bash case usage
bash case usage
2022-08-03 11:43:00 【little green head】
bash case usage
In bash scripts, in addition to using if for conditional judgment, you can also use case for conditional judgment.
case can handle many conditions than if else, and the conditions are all matching strings
case format is as follows
case variable inCondition 1)Statement 1statement 2;;Condition 2)Statement 1statement 2;;*)Statement 1statement 2;;esca
Note:
- Conditions are all strings, you can use simple bash wildcards such as |, [], etc. The * sign means match all strings
- Double quotes are required at the end of the statement;;
Example
Example 1: Write a script to provide the following menu to the user
m|M) show memory usages; show memory information
d|D) show disk usages; show disk information
q|Q)quit exit
[[email protected] bash_test]# cat 14.sh#!/bin/bashcat << EOFm|M) show memory usages;d|D) show disk usages;q|Q) quitEOFread -p "input you choice " UserChoicecase $UserChoice inm|M)free -m;;d|D)df -h;;q|Q)exit 0;;*)echo "invalid character"esac[[email protected] bash_test]# ./14.shm|M) show memory usages;d|D) show disk usages;q|Q) quitinput you choice Mtotal used free shared buff/cache availableMem: 7812 3227 4086 82 498 4247
Note: The option can use | to represent or, for example, input m or M to view memory information
Example 2: Write a program that reads input from the terminal and determines whether the input is a number, lowercase letter, uppercase letter, or special character
[[email protected] bash_test]# cat CheckInput.sh#!/bin/bashread -p "input a character:" Charcase $Char in[0-9])echo "a digit";;[A-Z])echo "a upper";;[a-z])echo "a lower";;[[:punct:]])echo "A punction.";;*)echo "Special char";;esac[[email protected] bash_test]# ./CheckInput.shinput a character:Aa upper
Note: [a-z] needs to be placed after [A-Z], because [a-z] includes [A-Z] in bash, but we can also use [[:lower:]] to represent lowercase letters, [[:upper:]]Represents capital letters
边栏推荐
猜你喜欢
随机推荐
距LiveVideoStackCon 2022 上海站开幕还有3天!
JS快速高效开发技巧指南(持续更新)
代码分析Objective-C中的深拷贝与浅拷贝
【MySQL功法】第2话 · 数据库与数据表的基本操作
零拷贝、MMAP、堆外内存,傻傻搞不明白...
flink流批一体有啥条件,数据源是从mysql批量分片读取,为啥设置成批量模式就不行
PC client automation testing practice based on Sikuli GUI image recognition framework
缓存--伪共享问题
[论文阅读] (23)恶意代码作者溯源(去匿名化)经典论文阅读:二进制和源代码对比
Redis发布订阅和数据类型
LyScript implements memory stack scanning
bash while循环和until循环
微信小程序获取用户手机号码
LeetCode-48. 旋转图像
c语言进阶篇:内存函数
FR9811S6 SOT-23-6 23V, 2A Synchronous Step-Down DC/DC Converter
【倒计时5天】探索音画质量提升背后的秘密,千元大礼等你来拿
《数字经济全景白皮书》金融数字用户篇 重磅发布!
数据库一席谈:打造开源的数据生态,支撑产业数字化浪潮
html+css+php+mysql实现注册+登录+修改密码(附完整代码)