Linux Shell script series tutorial (5): mathematical operations
This article mainly introduces the Linux Shell script series (V): mathematical operations. This article describes how to use let ,(()) and [] for arithmetic operations, use expr for arithmetic operations, use bc for arithmetic operations, you can refer to the following
In any programming language, arithmetic operations are essential, and shell is no exception.
I. Use let, (), and [] for arithmetic operations
You can use the normal variable value assignment method to define a value. This is, and it will be saved as a string. We can use let, (), [], and other operators to perform arithmetic operations on these variables. For example:
The Code is as follows:
#! /Bin/bash
No1 = 4 # Here, no1 is stored as a string
No2 = 5 # Here no2 is stored as a string
Let result = no1 + no2
Echo $ result # The output result is 9.
Let no1 ++ # is equivalent to let no1 = no1 + 1
Echo $ no1 # The output result is 5.
Let no2 -- # equivalent to let no2 = no2-1
Echo $ no2 # The output result is 4.
Let no1 + = 5 # equivalent to let no1 = no1 + 5
Echo $ no1 # The output result is 10.
Let no1-= 5 # It is equivalent to let no1 = no1-5
Echo $ no1 # The output result is 5.
No1 = 4 # Here, no1 is stored as a string
No2 = 5 # Here no2 is stored as a string
Result = $[no1 + no2]
Echo $ result # The output result is 9.
Result = $ [$ no1 + 5] # ubuntu cannot run normally, no1 not found
Echo $ result # The output result is 9.
Result = $(no1 + 50) # $ before parentheses cannot be lost; otherwise, an error is returned.
Echo $ result # output result 54
3. Use expr for arithmetic operations
The Code is as follows:
Expr is a more advanced operator than the preceding three operators, so you can watch for advanced operations. For example:
#! /Bin/bash
Result = 'expr 3 + 4' # It is not a single quotation mark, but a symbol of the key on the tab key.
Echo $ result # The output result is 7.
Result = 'expr 3 + 4' # Every space between 3 and +
Echo $ result # The output result is 3 + 4.
Note: Methods in 1.5.1 and 1.5.2 can only calculate integers, but cannot calculate floating point numbers.
Iii. arithmetic operations using bc
Bc is an advanced tool for mathematical operations. This precision calculator contains a large number of options. parameters are usually placed before the specific operation to be executed, and the semicolon is used as the delimiter, pass bc through sdtin. For example:
Echo "4*0.56" | bc # The output result is 2.24.
The Code is as follows:
No = 54
Result = 'echo "$ no * 1.5" | bc'
Echo $ result # The output result is 81.0.
Echo "scale = 2; 3/8" # The output result is 0.37 Based on the precision of scale decimal places
No = 100
Echo "obase = 2; $ no" | bc # hexadecimal conversion. The destination is binary. The default original is 10.
No = 1100100
Echo "obase = 10; ibase = 2; $ no" | bc # obase = 10, ibase = 2
Echo "sqrt (100)" | bc # Calculate the square root
Echo "10 ^ 10" | bc # calculates the square
-Note: The obase is the target base, and the ibase is the original base. By default, the original base is 10.