Shell scripts of conditional statements

Test conditions

1, the test document
2, an integer of test
3, the logic test string

if statement:

if a single branch statement
if dual branch statement
if a multi-branch statements
nested if statements

test command:
Test-specific expression is established when the condition is met, the test statement returns 0, otherwise other values.

Format 1: test conditional expressions
Format 2: [conditional expression] // Note that there are spaces

File test

Refers to the test document is based on the given path name, it is determined that the corresponding file or directory, or determining whether the file is readable, writable, executable, etc.

Common test operators:

-d: test whether a directory (Directory)
-e: to test whether a directory or file exists (Exist)
-f: Test whether the file (File)
-r: test whether the current user has permission to read (the Read)
-w: test whether the current user has permission to write (the write)
-x: to test whether the current user has permission to perform (eXcute)

Practice:
Formats:

test -d / etc / sysconfig (Test in / etc sysconfig directory exists)
echo $? (output value is 0, then the condition is true)
Shell scripts of conditional statements

Format II:

[-D / etc / sysconfig]
echo $? (Output value of 1, the conditions are not satisfied)
Shell scripts of conditional statements

Integer value comparison:
Format: [integer operator integer of 2]
Common test operators:

-eq: equal (Equal)
-ne: is not equal to (Not Equal)
-gt: greater than (the Greater Than)
-LT-: less than (Lesser Than)
-le: less than or equal to (Lesser or Equal)
-ge: greater than or equal to ( Greater or Equal)

String comparison

Format 1: [String String 1 = 2]
[String String = 1 2!]
Format 2: [String the -Z]
common test operator i:
=: the same string content
=:! String content ! different, represent opposite meaning
-z: the string is empty

Logic test

Format 1: [Expression 1] operator [Expression 2] ...
Format 2: Command 1 Command 2 ... operator
Common Options
-a or &&: logical AND, "and" means
-o or | |: logical or, "or" means
! : No logic, that "no" means

if statement:

Single branch structure :

if condition test operation
then
the command sequence
fi
Shell scripts of conditional statements
Shell scripts of conditional statements

Two-branch structure:

if condition test operation
then
the command sequence. 1
the else
command sequence 2
Fi
Shell scripts of conditional statements
Shell scripts of conditional statements

Multi-branch structure:

if condition test operation
then
the command sequence. 1
elif conditional test operation 2
then
command sequence 2
the else
command sequence. 3
Fi
Shell scripts of conditional statements
Shell scripts of conditional statements

if nested statements:

Purpose: jogging for the game, 10 seconds into the final, after entering the sex, were prompted to enter the men's or women's, to determine the use of multiple nested if realized
Shell scripts of conditional statements
Shell scripts of conditional statements

Guess you like

Origin blog.51cto.com/14449528/2440476