当前位置:网站首页>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
边栏推荐
- FR9811S6 SOT-23-6 23V, 2A Synchronous Step-Down DC/DC Converter
- JUC(三):锁核心类AQS ing
- OFDM 十六讲 4 -What is a Cyclic Prefix in OFDM
- 深入理解MySQL事务MVCC的核心概念以及底层原理
- App自动化测试怎么做?实战分享App自动化测试全流程
- 卷起来!阿里高工携18位高级架构师耗时57天整合的1658页面试总结
- 基于Sikuli GUI图像识别框架的PC客户端自动化测试实践
- 最牛逼的集群监控系统,它始终位列第一!
- fastposter v2.9.0 programmer must-have poster generator
- Simple implementation of a high-performance clone of Redis using .NET (1)
猜你喜欢
随机推荐
肝完Alibaba这份面试通关宝典,我成功拿下今年第15个Offer
LeetCode-48. 旋转图像
国内数字藏品与国外NFT主要有以下六大方面的区别
LyScript implements memory stack scanning
The effects of the background and the Activiti
hystrix 服务熔断和服务降级
下午见!2022京东云数据库新品发布会
零信任的基本概念【新航海】
bash if conditional judgment
从零开始Blazor Server(6)--基于策略的权限验证
Simple implementation of a high-performance clone of Redis using .NET (1)
LeetCode-142. 环形链表 II
Matlab学习13-图像处理之可视化GUI程序
What is a smart contract?
【JS 逆向百例】某网站加速乐 Cookie 混淆逆向详解
Matlab学习11-图像处理之图像变换
c语言进阶篇:内存函数
fastposter v2.9.0 programmer must-have poster generator
微信为什么使用 SQLite 保存聊天记录?
优维低代码:Provider 构件