Shell Script Theory & IF test conditions Part 2-- judge
Test conditions
We mentioned on a blog predefined variables in there $?变量
to determine whether the command is executed successfully. So how do you use it?
This time requires specialized test tools --test command, testing for specific conditions and uses $?
the returned value is determined, which command the following form:
test conditional expressions
&
[Conditional expression] // more common
Note: in brackets "[" and "]" between the need to use conditional expressions are separated by a space, or an error! !
First, the file test
format:
[Operator file or directory]
Common operator:
-d: test whether a directory
-e: the test file / directory exists
-f: test whether a file
-r: to test whether the current user has permission to read
-w: Testing whether the current user has write access
-x: Testing whether the current user has permission to execute
Instructions:
Second, the integer value test
format:
[Integer integer 1 Operand 2]
Common operator
-eq: equal
-ne: no equal
-gt: greater than
-lt: less than
-le: less
-ge: greater than or equal
Instructions:
Third, the string test
format:
[String String 1 Operator 2]
[String the -Z] // determines whether the contents of the string is empty
Common operator:
=: The same string content
! =: Different string content
Instructions:
Fourth, the logic test
format:
[Expression 1] operator [Expression 2]
&
Command 1 Command 2 Operators
Common test operators:
or -a &&: logical AND, "and" means
or -o ||: Logical OR, "or" means
! : Logical negation, conclusions negated
Instructions:
IF statement to determine
Single-branch structure
format:
if a conditional test operation
then the command sequence
be
Structure Flowchart:
Two-branch structure
format:
if a conditional test operation
then the command sequence 1
else command sequence 2
be
Structure Flowchart:
Multi-branch structure
format:
1 if condition test operation
then the command sequence 1
2, which test elif
then the command sequence 2
else command sequence 3
be
Structure Flowchart: