Numerical calculation double parentheses

Dual role parentheses is the numerical value of the comparison operation, its high efficiency, the use of very flexible, is often used by the operator.
((I = i + 1)) is the result of its role i + 1 is assigned to the variable i. Note that not use echo ((i = i + 1)) form the output value of the expression, but its value may be output using echo $ ((i = i + 1)).
i = $ ((i + 1)) is the action execution result i + 1 is assigned to the variable i.
((8> 5 7 && == 5)) before the comparison values, means for determining the condition.
echo $ ((2 + 1)) for outputting an operation expression results directly in (()) preceded by $ symbol.
 
Example 1 using (()) simple numerical calculation
[root@testdb ~]# echo $((1+1))
2
[root@testdb ~]# echo $((6-3))
3
[root@testdb ~]# ((i=5))
[root@testdb ~]# ((i=i*2))
[root@testdb ~]# echo $i
10
 
 
2. Examples of the use of (()) were slightly more complex integrated arithmetic
[root@testdb ~]# ((a=1+2**3-4%3))
[root@testdb ~]# echo $a
8
[root@testdb ~]# b=$((1+2**3-4%3))
[root@testdb ~]# echo $b
8
[root@testdb ~]# echo $((1+2**3-4%3))
8
[root@testdb ~]# a=$((100*(100+1)/2))
[root@testdb ~]# echo $a
5050
[root@testdb ~]# echo $((100*(100+1)/2)) 
5050
 
Example 3. A small sample special arithmetic operation symbol
[root@testdb ~]# a=8
[root@testdb ~]# echo $((a=a+1))
9
[root@testdb ~]# echo $((a+=1))
10
[root@testdb ~]# echo $((a**2))
100
 
Example 4 using (()) is determined compared double parentheses
[root@testdb ~]# echo $((3<8))
1
[root@testdb ~]# echo $((8<3))
0
[root@testdb ~]# echo $((8==8))
1
[root@testdb ~]# if ((8>7 && 5==5))
> then
> echo yes
> fi
yes
 
Example 5. Use around variables - Special expressions and operators ++
[root@testdb ~]# a=10
[root@testdb ~]# echo $((a++))
10
[root@testdb ~]# echo $a
11
[root@testdb ~]# a=11
[root@testdb ~]# echo $((a--))
11
[root@testdb ~]# echo $a
10
[root@testdb ~]# a=10
[root@testdb ~]# echo $a
10
[root@testdb ~]# echo $((--a))
9
[root@testdb ~]# echo $a
9
[root@testdb ~]# echo $((++a))
10
[root@testdb ~]# echo $a
10
 
[root@testdb ~]# myvar=99
[root@testdb ~]# echo $((myvar+1))
100
[root@testdb ~]# echo $(( myvar + 1 ))
100
[root@testdb ~]# myvar=$((myvar+1))
[root@testdb ~]# echo $myvar
100
 
Example 6. By (()) assigned to the variable operation
[root@testdb ~]# myvar=99
[root@testdb ~]# echo $((myvar+1))
100
[root@testdb ~]# echo $(( myvar + 1 ))
100
[root@testdb ~]# myvar=$((myvar+1))
[root@testdb ~]# echo $myvar
100
 
Example operator performs a variety of common commands Example 7 containing (()) of
[root@testdb ~]# echo $((6+2))
8
[root@testdb ~]# echo $((6-2))
4
[root@testdb ~]# echo $((6*2))
12
[root@testdb ~]# echo $((6/2))
3
[root@testdb ~]# echo $((6%2))
0
[root@testdb ~]# echo $((6**2))
36
 
Example 8. The various (()) of the shell script exemplary operation
[root@testdb ~]# cat test.sh
#!/bin/bash
a=6
b=2
echo "a-b=$(($a-$b))"
echo "a+b=$(($a+$b))"
echo "a*b=$(($a*$b))"
echo "a/b=$(($a/$b))"
echo "a**b=$(($a**$b))"
echo "a%b=$(($a%$b))"
[root@testdb ~]# sh test.sh
a-b=4
a+b=8
a*b=12
a/b=3
a**b=36
a%b=0
 
Example 9 Example 8 is rewritten into the form of a script parameter passing
[root@testdb ~]# cat test.sh 
#!/bin/bash
a=$1
b=$2
echo "a-b=$(($a-$b))"
echo "a+b=$(($a+$b))"
echo "a*b=$(($a*$b))"
echo "a/b=$(($a/$b))"
echo "a**b=$(($a**$b))"
echo "a%b=$(($a%$b))"
[root@testdb ~]# sh test.sh 8 2
a-b=6
a+b=10
a*b=16
a/b=4
a**b=64
a%b=0
[root@testdb ~]# sh test.sh 10 2
a-b=8
a+b=12
a*b=20
a/b=5
a**b=100
a%b=0

Guess you like

Origin www.cnblogs.com/liang545621/p/12610973.html