当前位置:网站首页>Conditional statements of shell programming
Conditional statements of shell programming
2022-07-04 01:33:00 【Official certification】
One 、 Conditions for testing
1、test command
Test whether the expression holds , If not, return 0, Otherwise, other values are returned
Format 1:test Conditional expression
Format 2:[ Conditional expression ]
- 1.
- 2.
2、 File test
[ The operator File or directory ]
- 1.
Common test operators



3、 Integer comparison
[ Integers 1 Operands Integers 2 ]
- 1.
Common test operators

①awk usage




②bc Tool usage

4、 String comparison
Format 1 [ character string 1 = character string 2 ]
[ character string 1 != character string 2 ]
Format 2 [ -z character string ]
- 1.
- 2.
- 3.
- 4.
Common test operators


5、 Logic test
Format 1:[ expression 1 ] The operator [ expression 2 ]
Format 2: command 1 The operator command 2
- 1.
- 2.
Common test operators

a=5
[ $a -ne 1 ] && [ $a != 2 ] Equate to [ $a -ne 1 -a $a != 2 ]
[ -d 11 ] && [ -f 2.txt ] && echo "yes"
[ -d 11 -a -f 2.txt ] && echo "yes"
[[ -d 11 && -f 2.txt ]] && echo "yes"
a=6
[ $a -ne 5 ] && [ $a -gt 5 ] && echo "yes"
[ $a -ne 5 -a $a -gt 5] && echo "yes"
[[ $a -ne 5 && $a -gt 5 ]] $$ echo "yes"
[[ $a -ne 5 || $a -gt 5 ]] && echo "yes"
[$a -ne 5 ] || [ $a -gt 5 ] && echo "yes"
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
&&、|| Operators can exist normally [[ ]] Conditional judgment structure , But if it appears in a [ ] In structure , You're going to report a mistake
example :

Two 、if Structure of statement
1、 Single branch structure
if Conditional test operation if Used disk >80%
then Command sequence then Call the police
fi fi
- 1.
- 2.
- 3.

Determine the mount point directory , If it doesn't exist, it will be created automatically



2、 Two branch structure
Determine whether the target host is alive or not , Display the inspection results
if Conditional test operation if 80 Whether the port is listening
then then
Command sequence 1 The website service is already running
else else
Command sequence 2 start-up httpd service
fi fi
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.

3、 Multi branch structure
if Conditional test operation if fraction 85-100 Between
then then
Command sequence 1 Judged as excellent
elif elif
Conditional test operation 2 Fraction in 70-84 Between
then then
Command sequence 2 It is judged as qualified
else else
Command sequence 3 Judged as unqualified
fi fi
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.

Example :
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 , And judge the gender of men and women , Boys enter the boys' group , Girls enter the girls' group , If the input is wrong, you will be prompted with an error .

3、 ... and 、case Structure of statement
case Multibranched structure
case A variable's value in
Model a )
Command sequence
;;
Model 2 )
Command sequence
;;
……
*)
Default command sequence
esac
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
case Statement notes
①case The line must begin with a word “in”, The first mock exam is a single bracket. ")" end
② Double a semicolon ";;" Indicates the end of the command sequence
③ In pattern string , You can use square brackets to indicate a continuous range , Such as "[0-9]"; You can also use vertical bars | Represents or , such as a|b
final *) Represents the default mode , Among them * It's equivalent to a wildcard
Example :
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 , And judge the gender of men and women , Boys enter the boys' group , Girls enter the girls' group , If the input is wrong, you will be prompted with an error .
Or the above question , Just use case Statement , Look at the diagram


summary
The syntax of conditional testing
File test 、 Integer comparison 、 String comparison 、 Logic test
if The syntax of conditional statements
Single branch 、 Double branch 、 Multiple branches
case The syntax of a multi branch statement
边栏推荐
- 【.NET+MQTT】.NET6 环境下实现MQTT通信,以及服务端、客户端的双边消息订阅与发布的代码演示
- Pratique technique | analyse et solution des défaillances en ligne (Partie 1)
- Decompile and modify the non source exe or DLL with dnspy
- PMP 考试常见工具与技术点总结
- Who moved my code!
- 基于.NetCore开发博客项目 StarBlog - (14) 实现主题切换功能
- Weekly open source project recommendation plan
- Ka! Why does the seat belt suddenly fail to pull? After reading these pictures, I can't stop wearing them
- Msp32c3 board connection MSSQL method
- C import Xls data method summary III (processing data in datatable)
猜你喜欢

I don't care about you. OKR or KPI, PPT is easy for you

JVM performance tuning and practical basic theory - medium
![Cesiumjs 2022^ source code interpretation [8] - resource encapsulation and multithreading](/img/d2/99932660298b4a4cddd7e5e69faca1.png)
Cesiumjs 2022^ source code interpretation [8] - resource encapsulation and multithreading

be based on. NETCORE development blog project starblog - (14) realize theme switching function

Three layer switching ②

Weekly open source project recommendation plan

查询效率提升10倍!3种优化方案,帮你解决MySQL深分页问题

Function: write function fun to find s=1^k+2^k +3^k ++ The value of n^k, (the cumulative sum of the K power of 1 to the K power of n).
![CesiumJS 2022^ 源码解读[8] - 资源封装与多线程](/img/d2/99932660298b4a4cddd7e5e69faca1.png)
CesiumJS 2022^ 源码解读[8] - 资源封装与多线程

How to delete MySQL components using xshell7?
随机推荐
File contains vulnerability summary
AI helps make new breakthroughs in art design plagiarism retrieval! Professor Liu Fang's team paper was employed by ACM mm, a multimedia top-level conference
查询效率提升10倍!3种优化方案,帮你解决MySQL深分页问题
All ceramic crowns - current market situation and future development trend
Future源码一观-JUC系列
在寻求人类智能AI的过程中,Meta将赌注押向了自监督学习
Skku| autonomous handover decision of UAV Based on deep reinforcement learning
Logical operator, displacement operator
Software product download collection
基于.NetCore开发博客项目 StarBlog - (14) 实现主题切换功能
Decompile and modify the non source exe or DLL with dnspy
Maximum entropy model
Day05 表格
All in one 1412: binary classification
Unity Shader入门精要读书笔记 第三章 Unity Shader基础
我管你什么okr还是kpi,PPT轻松交给你
Function: write function fun to find s=1^k+2^k +3^k ++ The value of n^k, (the cumulative sum of the K power of 1 to the K power of n).
LeetCode 168. Detailed explanation of Excel list name
Difference between value and placeholder
Introduction to superresolution