The variables in the shell are stored as strings, even if you assign values to the value of the shape unless you declare the variable by declare-i.
In the case of numerical/arithmetic operations, you may find the following
$ x=121
$ echo $x +1
121+1
Define the variable x and assign an integer variable, but the echo $x +1 is not the 122 we want, but the 121+1. This is because $x+1 is not an arithmetic operation, but rather a simple string operation.
So how do you perform numerical/arithmetic operations in a shell? (The shell used below is the bash shell)
Method One: Let command
$ let "x=x+1"
$ echo $x
122
Method Two: $[]
$ echo $[x-1]
121
$ echo $x
122
Method Three: $ (())
$ echo $ ((x-1))
121
$ echo $x
122
Method Four: Declare-i
$ declare-i X
$ echo $x +1
122+1
$ x= $x +1
$ echo $x
123
After you declare the variable x by declare, you need to assign a value to X to get the result of the arithmetic operation.
5. Add Double Bracket
Count=0
((count++))
6. Use the expr command
Count=0
Count= ' expr $count + 1 '
7. Use BC command
Count=0
Count= ' echo ' $count + 1 "|BC"
Introduced:
BC is a simple calculator under Linux, support floating-point calculation, the command line to enter the BC is entered into the calculator program, and we want to directly in the program floating-point calculation, the use of a simple pipe can solve the problem.
Attention:
1 The test BC supports all operators except the bitwise operator.
2 BC to use scale for precision setting
3 example of floating point count
count=3.11
Count= ' echo ' scale=2; $count *3 "|BC"
Echo $count
Output is 9.33
8. Use awk calculation
Echo $var 1| awk ' {printf ("%d\n", $1+$2)} '
Add:
((i= $j + $k)) equivalent to i= ' expr $j + $k '
((i= $j-$k)) equivalent to i= ' expr $j-$k '
((i= $j * $k)) equivalent to i= ' expr $j \* $k '
((i= $j/$k)) equivalent to i= ' expr $j/$k '
Let expressions execution of one or more expressions. You do not have to have $ before a variable in an expression. If the expression contains spaces or other special characters, it must be caused.
Example: let "i = i + 1" or let i=i+1
Arithmetic operators are operators that can add, subtract, multiply, and divide mathematical operations in a program. The mathematical operators commonly used in the shell are shown below.
-+: Add to two variables.
--: Subtract two variables.
-*: Multiply the two variables.
-/: Divide two variables.
-**: A power operation on two variables.
-%: Modulo operation, the first variable is divided by the second variable to find the remainder.
-+=: plus equals, adding a second variable on its own basis.
--=: Minus equals, subtracting the second variable on the basis of the first variable.
-*=: Multiply equals, multiply the second variable on the basis of the first variable.
-/=: In addition to equals, divide the second variable on the basis of the first variable.
-%=: Modulo assignment, the first variable takes modulo operations on the second variable, and then assigns the value to the first variable.
When using these operators, you need to be aware of the problem of the order of operations. For example, enter the following command to output the 1+2 result.
Echo 1+2
The shell does not output 3, but outputs the 1+2. There are three ways in the shell to change the order of operations.
-Change the order of operations with expr. You can use echo ' expr 1 +2 ' to output the result of 1+2, and expr to indicate that the following expression is a mathematical operation. Note that ' It's not a single quote, it's the symbol above the TAB key.
-use let to indicate mathematical operations. You can assign the result of the operation to the variable B first, and the Operation command is B=let 1 + 2. Then use echo$b to output the value of B. If there is no let, the 1+2 is output.
-use $[] to represent a mathematical operation. Writes a mathematical operation to the middle bracket of the $[] symbol, and the contents of the brackets are first mathematically calculated. For example, command echo$[1+2], outputs the result 3.
Here is a shell program instance that implements mathematical functions s=3 (XY) +4x2+5y+6 operations. Enter the values of x and Y in the program in the form of positional variables. The program's writing steps are shown below.
Open a terminal in the main menu. Enter VIM in the terminal by typing the VIM command.
In Vim, press "I" to enter insert mode, and then enter the following code.
Code 4-2 Examples of mathematical operations: \ Source File \04\4.4.sh
#!/bin/bash
#4.4.sh
S=0 #Define a summation variable, the initial value is 0.
t = 'expr $ 1 ** $ 2' #Change the order of operations with expr, find X's Y-side.
T = $ [T * 3] #t times 3.
S = $ [s + t] #Add the results.
T = $ [$ 1 ** 2] #Find the square of x.
T = $ [T * 4] #Multiply the result by 4.
S = $ [s + t] #Add the results.
t = 'expr $ 2 * 5' #Find the value of 5y.
S = $ [s + t] #Add the results.
S = $ [S + 6] #Result plus 6.
Echo $ s # Output results.
echo $ ((a% b)) #take the remainder
In this program, you need to pay attention to the writing of arithmetic operations. If no expr or $[] changes the order of operations, the expression is assigned as a string without the result of the assignment operation.
Press the "ESC" key to return to normal mode. Then enter ": W 4.4.sh" to save the file.
Enter the ": Q" command and press "enter" to exit vim.
In the terminal, enter the following command to add executable permissions to the 4.4.sh file.
chmod +x 4.4.sh
Enter the following command to run the program. You need to enter two parameters in the command.
./4.4.sh 2 4
The program completes the mathematical operation of the s=3 (XY) +4x2+5y+6 and outputs the results, as shown below.
90