当前位置:网站首页>Shell script explanation (II) -- conditional test, if statement and case branch statement
Shell script explanation (II) -- conditional test, if statement and case branch statement
2022-06-22 19:26:00 【Be the best Messi】
One 、 Conditions for testing
1、test command ( Special testing tools )
You can test specific conditions , And judge whether the condition is true according to the return value ( The return value is 0 Indicates that the condition is true )

2、 File test
File testing refers to testing based on a given path name , Determine whether the corresponding file or directory , Or judge whether the file is readable 、 Can write 、 Executable, etc .

| Common operators | explain |
|---|---|
| -d | Test whether it's a directory (Directory) |
| -e | Test whether the directory or file exists (Exist) |
| -f | Whether the test is a file (File) |
| -r | Test whether the current user has permission to read (Read) |
| -w | Test whether the current user has permission to write (Write) |
| -x | Test whether the current user has permission to execute (eXcute) |

3、 Integer comparison
Integer value comparison refers to the comparison between two given integer values , Judge the relationship between the first number and the second number .

| Common operators | explain |
|---|---|
| -eq | be equal to (Equal) |
| -ne | It's not equal to (Not Equal) |
| -lt | Less than (Leser Than) |
| -gt | Greater than (Greater Than) |
| -le | Less than or equal to (Lesser or Equal) |
| -ge | Greater than or equal to (Greater or Equal) |

4、 String comparison
String comparisons are often used to check user input 、 Whether the system environment meets the conditions , In the shell Script , It can also be used to judge whether the unknown parameters entered by the user meet the requirements .

| Common operators | explain |
|---|---|
| = | String content is the same |
| != | String content is different ,! The sign means the opposite |
| -z | The string content is empty |
| -n | Check if there is a string |
LANG Environment variables represent languages and character sets
PWD Indicates the current working directory
HOME Represents the user's Host Directory 
5、 Logic test
Determine the dependency between two or more conditions

| Common operators | explain |
|---|---|
| -a or && | Logic and , And it means |
| -o or ▕▕ | Logic or , Or it means |
| ! | Logical not |


Two 、if Conditional statements
1、 Single branch



2、 Double branch



3、 Multiple branches



4、 Example
①、 Example 1
- Check the... In the user's home directory test.sh Does the file exist , And check whether there is execution permission

②、 Example 2
Prompt the user for input 100 The number of seconds in the meter race , The number of seconds required is greater than the judgment 0 And less than or equal to 10 Seconds into the trials , Greater than 10 Seconds are eliminated , If you enter other characters, you will be prompted to re-enter ; The members who enter the trial will further judge the gender of men and women , Boys in the boys group , Girls in the girls group , If the input is wrong, please prompt the error .


③、 Example 3
- Prompt the user to enter content , Use if Statement to determine whether the input content is an integer , And determine whether it is even or odd

3、 ... and 、case sentence
1、 structure

case A variable's value in
Model a )
Command sequence
;;
Model 2 )
Command sequence
;;
*)
Default command sequence
esac
2、 Example :
①、 Example 1
- Prompt the user for input 100 The number of seconds in the meter race , The number of seconds required is greater than the judgment 0 And less than or equal to 10 Seconds into the trials , Greater than 10 Seconds are eliminated , If you enter other characters, you will be prompted to re-enter ; The members who enter the trial will further judge the gender of men and women , Boys in the boys group , Girls in the girls group , If the input is wrong, please prompt the error

②、 Example 2
- use case The statement is decompressed according to the suffix name .tar.gz or .tar.bz2 To /opt Catalog

Catalog
Two 、if Conditional statements
边栏推荐
- Centeros install mangodb
- China's games are "harvesting" foreigners
- Array implementation of circular linked list
- 2022 Chongqing preschool education industry exhibition 𞓜 hi tech Toy Puzzle decompression Toy Expo
- 贪心之区间问题(2)
- 5G 短消息解决方案
- Flutter系列-Dart基础语法学习
- Flutter系列-flutter路由管理
- IPLOOK 成为 RedHat(红帽)业务合作伙伴
- UE4_ Ue5 make 3dui follow the camera orientation (attached works)
猜你喜欢
Set of redis data structure

Linked list 4- 21 merge two ordered linked lists

What happened to this page when sqlserver was saving

Iplook 5gc successfully connected with CICA international CHF (billing function)
组合学笔记(五)分配格中的链

UE4_ Ue5 make 3dui follow the camera orientation (attached works)

JVM quick start

UE4_UE5制作3DUI-跟随相机朝向(附工程)

Several important viewpoints on operation and maintenance, monitoring and aiops

程序员工具大全【持续更新】
随机推荐
SRE必将走向混沌工程时代--华为云混沌工程实践
Method of activity jump to fragment (intent)
Postman learning
助力客户数字化转型,构建全新的运维体系
第八届 GopherChina 大会蓄势待发!
泡泡玛特:空洞的灵魂需要故事
IPLOOK 成为 RedHat(红帽)业务合作伙伴
上半年,这个领域竟出了7家新独角兽,资本争抢入局
中国两颗风云气象“新星”数据产品向全球用户共享
What happened to this page when sqlserver was saving
Flutter系列-flutter路由管理
2年狂赚178亿元,中国游戏正在“收割”老外
使能伙伴,春节重大保障“不停歇”
Chrome suddenly can't copy and paste
wpa_supplicant的状态机迁移
《被讨厌的勇气》读后感
200亿VS 50亿,“脱水”小红书,到底值多钱?
Robotframework installation tutorial
Custom database connection pool class: requirement: enclose the collection class of a collection object
在循环中动态改变标签元素的样式