Conditional expression:
1. File expression
# If the file exists if [-f file] # if the directory exists if [-d dir] # if the file exists and is not empty if [-s file] # if the file exists and is readable if [-r file] # if the file exists, you can write if [-w file] # if the file exists, you can run if [-x file]
2. Integer expression
# If a is equal to bif [a-eq B] # if a is not equal to bif [a-ne B] # if a is greater than bif [a-gt B] # if a is greater than or equal to bif [a-ge B] # if a is less than bif [a-lt B] # if a is less than or equal to bif [a-le B]
3. string variables
A = 'B = ''# if a is equal to bif [$ {a }=$ {B}] # if a is not equal to bif [$ {}! =$ {B}] # if a is not blank if [-n $ {a}] # if a is blank, the length is 0if [-z $ {a}]
= Put in if is the equivalent of a string, and assign values elsewhere. When equal signs are used as values and equals, there is a lot of difference in writing: there cannot be spaces on both sides of the equal signs. equal to equal signs, there must be spaces on both sides.
The method used to judge whether the string is not null is incorrect. However, this is true for many materials (not recommended ). The following method is tested:
If [$ {a}] is null, else is used.
4. logical expressions
4.1. Non-if [! Expression]
4.2.
If [expression-a expression] Or if [expression] & [expression]
echo 'write path:'read pathif [ -d ${path} -a -n ${path} ]then echo '---'else echo '+++'fi
Determines whether the input is a directory and not a blank string
Execution result :---
4.3, or
If [expression-O expression] Or if [expression] | [expression]