The basis of shell script programming-- arithmetic operation and logic operation
I. the logical relationship between commands:
Logic and: &, the previous expression is false, the result is false; the second paragraph no longer needs to be judged, that is, the second paragraph is no longer running. The previous expression is true, and then run the next expression, and if the next expression is also true, the result is true.
Summary: two expressions run and operate. If two expressions are true, the result is true; otherwise, it is false.
Logical OR: | |, the previous expression is true, the result is true, and the second paragraph is no longer judged. If the first expression is false, the next expression is run, the second expression is false, the result is false, the second expression is true, and the result is true.
Summary: if two expressions run or operate, if one expression is true, the result is true; if both expressions are false, the result is false.
Example:
[$FINLINES-gt] & & echo "/ etc/inittab is a big file." | | echo "/ etc/inittab is a small file"
-1-2-3-
Description: when 1 is true, the result of 2 / 1 and 2 / 2 is true, followed by or operation, that is, paragraph 3 is no longer executed.
When 1 is false, it is followed by the operation, paragraph 2 is not executed, 1 and 2 are false and the result is false, followed by or, execute paragraph 3
2. Arithmetic operation
How to do arithmetic in shell: shell itself treats all variables as characters. If you want to do arithmetic, you need to do extra processing. Multiplication symbols, using *, may sometimes need to be escaped.
For all of the following results, you can use echo $C to see the results of variable C.
1. Let arithmetic expression
Atom 3
Bamboo 6
Let C=$A+$B # let do arithmetic, add, subtract, multiply and divide
2. $[arithmetic expression]
Cations $[$Amoy B]
3. $((arithmetic expression))
C $(($Aids B)
4. Expr arithmetic expression with spaces between operands and operators in the expression and using command reference
C = `expr $A + $B `
Dashed $(expr $A + $B) # here, there should be spaces on both sides of the operator.