当前位置:网站首页>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
边栏推荐
- Lightweight Pyramid Networks for Image Deraining
- Design of database table foreign key
- Solution of cursor thickening
- Function: find the sum of the elements on the main and sub diagonal of the matrix with 5 rows and 5 columns. Note that the elements where the two diagonals intersect are added only once. For example,
- Thinkphp6 integrated JWT method and detailed explanation of generation, removal and destruction
- Oracle database knowledge points that cannot be learned (III)
- 查询效率提升10倍!3种优化方案,帮你解决MySQL深分页问题
- Gee: create a new feature and set corresponding attributes
- Huawei rip and BFD linkage
- Future源码一观-JUC系列
猜你喜欢
2-redis architecture design to use scenarios - four deployment and operation modes (Part 2)
Introduction to unity shader essentials reading notes Chapter III unity shader Foundation
[common error] UART cannot receive data error
ES6 deletes an attribute in all array objects through map, deconstruction and extension operators
Pyinstaller packaging py script warning:lib not found and other related issues
MySQL deadly serial question 2 -- are you familiar with MySQL index?
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
How can enterprises optimize the best cost of cloud computing?
Huawei cloud micro certification Huawei cloud computing service practice has been stable
GUI application: socket network chat room
随机推荐
Trading software programming
ES6 deletes an attribute in all array objects through map, deconstruction and extension operators
1-redis architecture design to use scenarios - four deployment and operation modes (Part 1)
MPLS experiment
Force buckle day32
7.1 学习内容
TP5 automatic registration hook mechanism hook extension, with a complete case
Introduction to superresolution
mysql使用視圖報錯,EXPLAIN/SHOW can not be issued; lacking privileges for underlying table
Pesticide synergist - current market situation and future development trend
Function: find the approximate value of the limit of the ratio of the former term to the latter term of Fibonacci sequence. For example, when the error is 0.0001, the function value is 0.618056.
Huawei BFD and NQA
Special copy UML notes
Oracle database knowledge points that cannot be learned (II)
Msp32c3 board connection MSSQL method
About uintptr_ T and IntPtr_ T type
GUI 应用:socket 网络聊天室
Day05 表格
Douban scoring applet Part-3
Human resource management online assignment