Linux | shell script - Compare judgment and sentence operations

I. Introduction

In the process we wrote a shell script is often necessary to make comparative judgments, such as relatively large numbers which determine whether a file exists, determine whether the file is a directory, and other operations to determine whether it is true. Sometimes we need to do to run, such as addition, subtraction, etc., today to brief comparison expressions and expressions run.

Two, shell comparison determination

1, file status:
the expression: [file path -e] have space-separated
example: [-e / root] Analyzing / root whether there is
(1) -e is true whether or not there exists
(2) -d is a directory is Catalog is true
(3) -f whether the file is a true file
(4) -x whether the Executive authority Executive authority is true
(5) -w whether written authority has the right to write is true
(6) -r whether there read access permissions have read is true

2, the integer value comparison
[Option num1 num2]
For example: [2 -eq 2], or by the variable [-eq $ A $ B]
(. 1) is equal to -eq
(2) is not equal to -ne

(3) -gt greater than
(4) -ge than or equal to
(5) -lt less than
(6) -le less

3, character comparison
[ "string string 1" option "string String 2"]
For example: [$ name == "jim" ]
Note: The options and there are spaces on both sides of the string! ! ! ! Must lead character in double quotation marks.
(1) = = equal equal to the true
(2)! = True is not equal unequal 0
(. 3) is true empty the -Z
(4)! -z non-empty non-empty true

Using 4, when the logic determines that two or more :( logical comparison determination condition)
(1) and Logical:
&& plurality of conditions are true [Condition 1] && [Condition 2]
(2) or logic:
|| more conditions would be as long as one condition can be [condition. 1] || [condition 2]
(3) non-logic! = Negated
PS: two expressions:
.. 1 && ---- [-gt 10. 8] && [-LT-. 5. 6] -a 2. Usage: [-LT-10 -gt. 8. 6. 5 -a]
. 1. || ---- [10 -gt 8] || [5 -lt 6] 2. -o usage: [-LT-10 -gt. 8 -0. 5. 6]
(. 4) when calling the variable " 'and s' the difference between
the characters are the same two output, when the variable output single quote character, variable function using double quotes
example: a = 10 B = 10
"+ $ a $ B" = num2 result num2 = 10 + 10
'$ a + $ b '= num2 result $ a + $ b = num2
Linux | shell script - Compare judgment and sentence operations

Three, shell operations grammar

1, the operator
plus +
subtraction -
multiplication *
addition /
modulo%
increase since 1 ++ i = 0; let i ++; echo $ i
decremented 1 - j = 10; let j-- ; echo $ j

2, the operation instruction \
(1) operator command expr: + - * /%
expr integer of 1 ... 2 integer values provided by the integer variable, given directly calculation result
expr $num1 + $num2
(2) the let command
manipulated variable value, only the calculation can not be the output
I = 0; the let I ++; echo $ I
Linux | shell script - Compare judgment and sentence operations
I = 0; I ++; echo $ I
Linux | shell script - Compare judgment and sentence operations
(. 3) using the $ [] or $ (()) expression (need to use echo output, the operation type and expr similar to)
the PS : multiplication does not need an escape character \
use variable, the variable name specified directly, without adding the symbol $
echo $ []
echp $ (())
. 1) example: a = 10 b = 10
is not an escape: \
Linux | shell script - Compare judgment and sentence operations
to rotate the Meaning: \
Linux | shell script - Compare judgment and sentence operations
2) a = 10 B = 10
echo $ ((a
B))
Linux | shell script - Compare judgment and sentence operations
is incremented (4) variables, and other operations decrement
simplified expression:
+ = increase since each x x
- x = x decremented each time
---- = I 2 + I> I 2 + =
I = I-2 ----> 2 I- =
I-I. 1 ---- => i--
I = I 2 ----> I 2 =
I = I +. 1 ----> ++ I
I = 2% I ----> I = 2%

(5) practical example: Math Calculator
vim test.sh
Linux | shell script - Compare judgment and sentence operations
operating results:
Linux | shell script - Compare judgment and sentence operations

Fourth, the redirection (change data output device)

1, the output redirection
> coverage

>> Additional

2, redirect input

<Coverage

<< Add

3, redirect error output

2> coverage

2 >> Add

4, redirect the output and error output

&> Override

&> Append

5, / dev / null device a Black Hole

Fifth, combat

(1) Use & and || scripts, redirection of use

Behind successful operation performed before the foregoing && continue, whereas the latter operation is not performed

Examples: ping -c 3 1.1.1.1 &> / dev / null && echo "on line" (output of the ping command line does not result directly to the output)
Linux | shell script - Compare judgment and sentence operations

Examples: ping -c 3 121.201.88.88 2> / dev / null && echo "on line" (This results in the output of the ping command, and outputs the result)
Linux | shell script - Compare judgment and sentence operations

Success is nothing output to output the contents of failure

Ping failed, not content output
Linux | shell script - Compare judgment and sentence operations

|| foregoing operations performed, the operation was performed later. If the foregoing operation is executed successfully, the latter operation is not performed.

ping -c 2 121.201.88.89 || echo “pc not online”
Linux | shell script - Compare judgment and sentence operations

(2) combat: detecting whether the host online

vim test.sh
Linux | shell script - Compare judgment and sentence operations

Next issue: shell script - if conditional statements and loops for
Linux | shell script - Compare judgment and sentence operations
Rui Jiang cloud computing official website link: https://www.eflycloud.com/home?from=RJ_0024

Guess you like

Origin blog.51cto.com/13475644/2400518