当前位置:网站首页>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
边栏推荐
- Huawei BFD and NQA
- Conditional test, if, case conditional test statements of shell script
- 机器学习基础:用 Lasso 做特征选择
- C import Xls data method summary V (complete code)
- Function: store the strings entered in the main function in reverse order. For example, if you input the string "ABCDEFG", you should output "gfedcba".
- Fundamentals of machine learning: feature selection with lasso
- Use classname to modify style properties
- Some other configurations on Huawei's spanning tree
- be based on. NETCORE development blog project starblog - (14) realize theme switching function
- mysql使用視圖報錯,EXPLAIN/SHOW can not be issued; lacking privileges for underlying table
猜你喜欢

Ka! Why does the seat belt suddenly fail to pull? After reading these pictures, I can't stop wearing them

Fundamentals of machine learning: feature selection with lasso

Pratique technique | analyse et solution des défaillances en ligne (Partie 1)

MySQL deadly serial question 2 -- are you familiar with MySQL index?

51 MCU external interrupt

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

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,
![[common error] custom IP instantiation error](/img/de/d3f90cd224274d87fcf153bb9244d7.jpg)
[common error] custom IP instantiation error
![CesiumJS 2022^ 源码解读[8] - 资源封装与多线程](/img/d2/99932660298b4a4cddd7e5e69faca1.png)
CesiumJS 2022^ 源码解读[8] - 资源封装与多线程

MySQL statement learning record
随机推荐
Print diamond pattern
Introduction to A-frame virtual reality development
mysql使用视图报错,EXPLAIN/SHOW can not be issued; lacking privileges for underlying table
CLP information - how does the digital transformation of credit business change from star to finger?
【.NET+MQTT】. Net6 environment to achieve mqtt communication, as well as bilateral message subscription and publishing code demonstration of server and client
Future源码一观-JUC系列
基于.NetCore开发博客项目 StarBlog - (14) 实现主题切换功能
Logical operator, displacement operator
Summary of JWT related knowledge
查询效率提升10倍!3种优化方案,帮你解决MySQL深分页问题
Design of database table foreign key
C library function int fprintf (file *stream, const char *format,...) Send formatted output to stream
Since the "epidemic", we have adhered to the "no closing" of data middle office services
Development of user-defined navigation bar in uniapp
Avoid playing with super high conversion rate in material minefields
Force buckle day32
Fundamentals of machine learning: feature selection with lasso
Decompile and modify the non source exe or DLL with dnspy
String hash, find the string hash value after deleting any character, double hash
Do you know the eight signs of a team becoming agile?