Home > Article > Backend Development > Use if conditional statement
Using professional if conditional statements can better organize the script structure, making it clear and easy to understand.
【Structure of if statement】
(1) Single-branch if statement
Structure: if conditional test operation For example: if disk used space
then command sequence then Call the police
的 (2) IF statement of double branches
Structure: IF condition test operation, such as: whether the IF 80 is listening to
The then command sequence 1 TheN website service is running
ELSE command sequence 2 ELSE starts HTTPD service HTTPD service三 Fi Fi) (3) Multi-branch IF statement
Structure: IF condition test operation 1 For example: IF score between 85-100?测试 ELIF condition test operation 2 ELIF score between 60-84?
TheN command sequence 2 then is judged as qualified
Esle Esle
command sequence 3 is judged as unqualified
... ...
I Fi Fif [IF statement application example]
(1) Single -branch IF statement application
Add execution rights and verify:
(2) Double branch if statement application
Write a script to check whether the vsftpd service is running through the Shell script. If it is already running, list its listening address and PID number. Otherwise, the output prompt will be "Warning: vsftpd service is unavailable!"
Input: vim chkftpd.sh
Add execution permissions and test the effect
(3) Multi-branch if statement application
Write a script to classify grades, and distinguish excellent, qualified, and unqualified grades based on the input test scores. .
Input: vim gradediv.sh
Add execution permissions and verify the effect: