The actual use of IF conditional statement of Shell programming in Linux system
Conditional statement conditional Test test Command for Shell programming
Test whether a specific expression is valid. When the condition is true, the return value of the test statement is 0, otherwise it is other values.
Format 1: test conditional expression format 2: [conditional expression]
File test [operator file or directory] A common test operator.
(1)-d: test whether it is a directory (Directort)
(2)-e: test whether a file or directory exists (Exist)
(3)-f: test whether it is a file (File)
(4)-r: test whether the current user has permission to read (Read)
(5)-w: test whether the current user has permission to write (Write)
(6)-x: test whether the current user has permission to execute (eXcute)
Integer value comparison [integer 1 operator integer 2] commonly used test operators
(1)-eq: equal to (Equal)
(2)-ne: not equal to (Not Equal)
(3)-gt: greater than (Dreater Than)
(4)-lt: less than (Leaser Then)
(5)-le: less than or equal to (Lesser or Equal)
(6)-ge: greater than or equal to (Greater or Equal)
String comparison format 1: [string 1 = string 2] [string 1! = string 2] format 2: [- z string] commonly used test operators
(1) =: the string content is the same
(2)! =: the string content is different! The sign means the opposite.
(3)-z: the string content is empty
Logical test format 1: [expression 1] operator [expression 2] format 2: command 1 operator command 2. Commonly used test operators
(1)-an or & &: logic and the meaning of "and"
(2)-o or | |: logical or, "or"
(3)! Logical no
If statement if single loop statement if conditional test operation then command sequence fiif single loop structure diagram
Determine the mount point directory and create it automatically if it does not exist
#! / bin/bashMOUNT_DIR= "/ meida/cdrom" if [!-d $MOUNT_DIR] then echo "mount point does not exist" mkdir-p $MOUNT_DIRfi
If double branch structure if conditional test operation then command sequence 1 else command sequence 2fiif double branch structure diagram
Determine whether the target host is alive or not, and display the check results.
#! / bin/bash# determines whether the target host is alive or not, and displays the test result ping-c 3-I 0.2-W 3 $1 & > / dev/nullif [$?-eq 0] then echo "Host $1 is up." / / the output result else echo "Host $1 is down." / / the output result of the host that does not survive
Ping-c 3 (number of packets sent)-I 0.2 (every 0.2 seconds)-W 3 (wait 3 seconds) $1 & > / dev/null
If multi-branch structure if conditional test operation 1 then command sequence 1elif conditional test operation 2 then command sequence 2else command sequence fiif multi-branch structure diagram
Judge the range of scores and divide them into excellent, qualified and unqualified third gears
#! / bin/bashread-p "Please enter your score (0,100):" GRADEif [$GRADE-ge 85] & & [$GRADE-le 100] then echo "$GRADE score, excellent!" Elif [$GRADE-ge 70] & & [$GRADE-le 84] then echo "$GRADE score, qualified!" Else echo "$GRADE score, not qualified!" Fi