13 Basic IF statements
Format 1:if condition; Then statement; Fi
Execution result is 5
Format 2:if condition; Then statement; else statement; Fi
#!/bin/basha=5if [ $a -gt 10 ];then echo $aelse echo 0fi
Execution result is 0
Format 3:if ...; Then ...; Elif ...; Then ...; else ...; Fi
#!/bin/basha=8if [ $a -lt 2 ];then echo $aelif [ $a -gt 10 ];then echo 10else echo 0fi
Execution result is 0
Note: If logical judgment, the top-down satisfies the condition to perform the corresponding action immediately after the end. If not satisfied, continue to judge until the condition is met.
Logical judgment expression: if [$a-gt $b]; If [$a-lt 5]; If [$b-eq 10] et-gt (>); -lt (<); -ge (>=); -le (<=);-eq (= =); -ne (! =) Note that there are spaces everywhere
can use && | | Combine multiple conditions
If [$a-gt 5] && [$a-lt 10]; Then
If [$b-gt 5] | | [$b-lt 3]; Then
Two if determine file, directory properties
[-F file] Determines if it is a normal file, and there is
[-D file] Determines if it is a directory and exists
[-E file] to determine whether files or directories exist
[-R File] to determine if the document is readable
[-W file] Determines whether the file is writable
[-X file] Determines whether the file is executable
A few special judgments
If [-Z ' $a '] This indicates what happens when the value of variable A is empty
If [-n ' $a '] means that the value of variable A is not empty
If Grep-q ' 123 ' 1.txt; Then what happens if the 1.txt contains a ' 123 ' row
if [!-e file]; Then what happens when the file doesn't exist?
if (($a <1)); Then ... Equivalent to if [$a-lt 1]; Then ...
Symbols such as <,>,==,!=,>=,<= cannot be used in []
Case judgment in the three shells
Format case variable name in
value1)
Command
;;
value2)
Command
;;
*)
Commond
;;
Esac
In a case program, you can use a |, meaning, or means, such as 2|3, in a condition to command;
Example
#!/bin/bashread -p "Please input a number: " ncase $n in1) ls /root/;;2) fdisk -l;;3) ifconfig;;esac
After the script runs, you can enter 1 or 2 or 3 to execute the corresponding command.
Linux Learning Summary (58) Shell Script 2-Logical judgment if