Shell script programming [calculation operation + conditional comparison + conditional if]

Integer operations
Basic operation types
Four arithmetic operations
Addition: num1+num2
Subtraction: num1-num2
Multiplication: num1*num2
Divide: num1/num2

Modulo: num1%num2


The expr operation tool
calculates the specified expression and outputs the result
Format : expr integer 1 operator integer 2...
Multiplication operations should be escaped with \* to avoid being used as shell wildcards
expr 43 + 21 (expr $X + $Y)
expr 43 \* 21 (expr $X \* $Y)
expr 43 / 21 (expr $X / $Y)
expr 43% 21 (expr $X % $Y)


$[] arithmetic substitution
using $[integer1 operator Integer 2 .. ..]
Multiplication operation * No escaping required, no spaces on both sides of the operator The $ sign can be omitted for
referencing variables The
calculation result replaces the expression itself, which can be output in combination with the echo command Word increment/decrement operations of


variables
use $ [] replacement, or let command to complete
. Combine echo command to view the result
Shorthand expression Complete expression
   i++ i=i+1
   i-- i=i-1
   i+=2 i=i+2
   i-=2 i=i- 2
   i*=2 i=i*2
   i/=2 i=i/2

   i%=2      i=i%2


Limitations of decimal operations
Integer operations The
built-in mechanism of bash only supports integer value operations-
expr command, $[] formula replacement does not support operations with decimals


Use
bc to implement decimal operations Most Linux systems install this tool by default
--supports high-precision numbers Operation
-- directly operate bc to enter the interactive operation interface, quit to exit
Set scale=n to constrain the decimal place
Send expressions to bc in combination with pipelines
- multiple expressions are separated by semicolons
- pass the expression to be calculated through the echo command + pipeline


Formula
Basic usage of decimal value comparison:
-echo "value 1 comparator value 2" | bc
- If the expression is true, the returned result is 1, otherwise it returns 0
- Common comparison operations: > ,>=,<,< =,==,!=


Conditions test
the intelligence
of Provide the most direct identification basis for the execution of commands
- the read/write status of files or directories
- the size of the value
- whether the string matches
- a combination of multiple conditions


Common tests Operation
string comparison
[operator string]
-z The value of the string is empty
-n The value of the string is not empty (equivalent to!-z)
== Two strings are the same
! = the two strings are not the same


Integer value comparison
-eq equal to
-ne not equal to
-ge greater than or equal to
-le less than or equal to
-gt greater than
-lt less than


file
status test -e to determine whether the object exists, if it exists, the result is true
-d to determine whether the object is a directory, True
if it is -f Whether the object is a general file, true if it is
-r Determine whether the object has readable permission, and true if it is
-w Determine whether the object has writable permission, it is true

-x Whether the object 1 has executable permission, true if it is


Combining multiple conditional

logical separation operations

&& Given conditions must all be true, the entire test result is true
|| As long as one of the conditions is true, the entire test result is true


If selection structure
Single branch structure
Syntax format and characteristics
"When "condition is true "When the command sequence is executed
" otherwise, do nothing
if condition test
  then command sequence
fi


double branch structure
When "condition is true" execute command sequence 1
otherwise, execute command sequence 2
if condition test
 then command sequence 1
  else command sequence 2
fi Syntax format and characteristics of


multi-branch structure It is equivalent to nesting of if statements . Perform different operations for multiple conditions. If condition test 1  then command sequence 1 elif condition test 2  then command sequence   else command sequence fi


































































































































































































































































Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324846188&siteId=291194637