shell(2) Numerical operations

1. The usage of expr is troublesome and rarely used.
Example:
[jamin@localhost ~]$ expr 1 + 2
3
+ signs must be surrounded by spaces

[jamin@localhost ~]$ expr 3 \* 2 
6
* signs are special characters. 2. Example

using [ ] : [jamin@localhost ~]$ echo $[ 1 + 2 ] 3 [jamin@localhost ~]$ echo $[ 3 * 2 ] 6 Use in shell: #!/bin/bash a=100 b=2 c=5 echo $[ $a - $[$b * $c] ] 3. Use double brackets (()) $ echo $((3 + 2)) 5 echo $(( 3 * 2 )) 6 Use in shell: #!/bin/bash a=100 b=2 c=5 echo $(( $a - $(($b * $c)) ))
























Division is not listed in the example above because the shell only supports integer arithmetic.


Mathematical expression with double brackets (( expression ))

Command symbols that can be used
var ++ Increment after
var-- Subtract after
++ Bit Boolean OR && Logical AND || Logical OR Example: $ echo $((100 ** 2)) 10000














Note: Can also be used in an if statement. 

Simple example:

#!/bin/bash
a=100
if (( $a ** 2 > 1000 ));then
    a2 = $(($a ** 2))
    echo "100 square is $a2"
fi


The for loop calculates the sum of 1...10

#!/bin/bash

sum=0
for (( i=1;i<11;i++ ))
do
    sum=$(($sum+$i))
done
echo $sum


Floating point arithmetic solution
1, bash calculator bc. A programming language in bc that allows input of floating point numbers for computation.
Install bc
$sudo yum install bc -y




Example: first enter the bc command
$ bc #bc -p to remove the following welcome message
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc .
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'. 

2 + 3
5
2 * 3
6
2 - 3
-1

2.2 * 3.3
7.2
2/3
0

In the above example, multiplication and division do not get us desired result.
Floating point arithmetic is controlled by the built-in variable scale. You must set this value to the number of decimal places you want to keep in the calculation result, otherwise you won't get the desired result.
Then the above example
scale=4
2.2 * 3.3
7.26


a=100 #After the variable is defined, it can be used in the entire bc session. Re-enter bc, the variable disappears
b=5
c=2
print a #print can print the value of the variable

Use bc
example in shell:
#!/bin/bash
var=$(echo "scale=4;a=10;b=3 ; a / b" | bc)
echo $var
Note: define multiple variables separated by ';', variables a, b can also be defined in the shell. As follows:
#!/bin/bash
a=10
b=3
var=$(echo "scale=4; $a / $b " | bc)
echo $var


When the calculation formula is long, the above processing is inconvenient. The bc command recognizes input redirection, but it is not advisable to write the expression to a file.
Your best bet is to use inline input redirection, which allows you to redirect data directly on the command line.
Example: Calculate the sum of 1...100 using the arithmetic sum formula (S = n*a1 + n(n-1)*d/2 )


#!/bin/bash
a1=1
n=100
d=1
sum=$(bc << EOF
sum_1 = $n * $a1
sum_2 = $n * ($n - 1) * $d / 2
sum_1 + sum_2
EOF
)
Note: The EOF text string identifies the start and end of the inline redirection data. Command substitution notation is still required to assign the output of the bc command to a variable

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326764158&siteId=291194637