1. Condition Judgment:
If use:
if condition; Then commands; fi
If else uses:
if condition; Then commands; Else if Then commands; Else commands; fi
Description
&&| | action; # If condition is false, the action is executed. && is the logic and operator, | | is a logical OR operator. This is a useful technique when writing bash scripts. Now let's look at the conditions and comparison operations.
2. Arithmetic comparison
conditions are usually placed inside enclosed brackets. Be sure to note that there is a space between [or] and the operand. If you forget this space, the script will get an error. For example: [$var-eq0] or [$var-eq0] To determine the arithmetic condition of a variable or value: [$var-eq0] #当 $var equals0returns True when the [$var-ne0] #当 $var for non-0, return True other important operators as shown below.-GT: Greater than.?-LT: less than.?-GE: Greater than or equal to.?-le: Less than or equal to. You can test with multiple conditions as follows: [$var 1-ne0-A $var 2-GT2] #使用逻辑与-a[$var 1-ne0-O VAR2-GT2] #逻辑或-O
3. File System related test
we can use different criteria flags to test different file system-related properties. [ -f $file _var]: Returns True if the given variable contains a normal file path or filename. [ -x $var]: Returns True if the given variable contains a file that is executable.? [ -d $var]: Returns True if the given variable contains a directory. [ -e $var]: Returns True if the given variable contains a file that exists.? [ -c $var]: Returns True if the given variable contains a path to a character device file.? [ -b $var]: Returns True if the given variable contains a path to a block device file. [ -W$var]: Returns True if the given variable contains a file that is writable.? [ -R $var]: Returns True if the given variable contains a file that is readable. [ -L $var]: Returns True if the given variable contains a symbolic link. Here's how to use it: Fpath="/etc/passwd"if[-e $fpath]; ThenEchoFile exists;ElseEchoDoes not exist;fi
4. String comparison
======> <-n $str 1]]: Returns True if STR1 contains a non-empty string.
Conditional judgment in Linux Shell corner