The bc command linux

When the script to be processed in a floating-point calculation, the calculator can bc used, look at the following example

[root@node2 tmp]# cat bc.sh
#!/bin/bash
#
num1=`echo "scale=3; 4.3/7" | bc`
echo $num1
[root@node2 tmp]# ./bc.sh
.614
 

Short when it comes to calculation, this method can be used

variable=`echo "options; expression" | bc`
 

 

But if you calculate more complicated, in the same command lists multiple-time

Expression would be more trouble. At this point, you can enter the bc command redirection.

Using inline input redirection:

variable=`bc << EOF
options
statements
expressions
EOF
`
 

Look at the following example

[root@node2 tmp]# cat bc.sh
#!/bin/bash
#
num1=14.5
num2=5
num3=32.4
num4=`bc << EOF
scale=4
a=($num1 / $num2)
b=($num3 / $num2)
a + b
EOF
`
echo $num4
[root@node2 tmp]# ./bc.sh
9.3800
 

Note that the calculator created in bash variables a and b in a shell script can not be used.

 

var2=$(bc << EOF

 

scale = 4

 

a = ( $v1 + $v2 )

 

b = ($ v3 * $ v4)

 

a * b + 15.35

 

EOF

 

)

 

Guess you like

Origin www.cnblogs.com/igoodful/p/11443735.html