Mathematical calculations in the shell
One, use square brackets
#!/bin/Basha=tenb=c=res=$[$a * ($c-$b)] Echo $res
Ii. usage of BC
BC Technology is actually a programming language that recognizes:
numbers (integers and floating-point numbers)
Variable (simple variable and array)
Note (# c language /* */ start line)
expression
programming statements (e.g. if-then statement)
functions
floating-point operations are built-in variables Span class= "Fontstyle1" >scale controlled. You must set this value to the number of decimal
digits that you want to keep in the calculation results, otherwise you will not get the desired result.
/span>
$ BC-q3.4450scale =43.445. 6880 quit$
BC can also support variables:
$ BC-qvar1=45Print var2 2quit$
Use BC in scripts:
Way One:
variable=$ (echo "options; Expression "| bc
Instance:
#!/bin/bashvar1=$ (echo"scale=4; 3.44/5" | BC) Echo The answer is $var 1
Mode two, using inline input, format:
variable=$ (BC << EOF
Options
Statements
Expressions
Eof
)
var1=10.46var2=43.67var3=33.2var4=inVAR5 =$ (BC <<4= ($var 1 *= ($var 3 *+ b1eof)
Shell Script Note (iii) mathematical calculations in the shell