Shell scripts (conditional test & IF judge) - theory part 2

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:

Shell scripts (conditional test & IF judge) - theory part 2

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:

Shell scripts (conditional test & IF judge) - theory part 2

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:

Shell scripts (conditional test & IF judge) - theory part 2

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:

Shell scripts (conditional test & IF judge) - theory part 2

IF statement to determine

Single-branch structure

format:

if a conditional test operation

then the command sequence

be

Structure Flowchart:

Shell scripts (conditional test & IF judge) - theory part 2

Two-branch structure

format:

if a conditional test operation

then the command sequence 1

else command sequence 2

be

Structure Flowchart:

Shell scripts (conditional test & IF judge) - theory part 2

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:

Shell scripts (conditional test & IF judge) - theory part 2

To be continued ~ ~ ~ ~

Guess you like

Origin blog.51cto.com/14484404/2440296