Home  >  Article  >  Backend Development  >  Use if conditional statement

Use if conditional statement

高洛峰
高洛峰Original
2016-11-03 17:21:581859browse

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 Use if conditional statement

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

Use if conditional statement command sequence 3 is judged as unqualified

...

...
I Fi Fif [IF statement application example]

(1) Single -branch IF statement application

Written a script, you can first determine whether the current user is root, if not, it is not reported and executed " exit 1" exits the script (1 represents the return status value after exit) without executing other code.


Input: vim /opt/chkifroot.sh



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

Use if conditional statement

Add execution permissions and test the effect

Use if conditional statement

(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

Use if conditional statement

Add execution permissions and verify the effect:

Use if conditional statement

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn