The percentage of CPU, memory usage of the process seen through the top command is a floating-point number, and I need to process it while writing the script, so I learned a few things and summarized as follows.
In fact, the Shell (here is bash) itself does not have the ability to handle floating-point computations, but can use the "BC" This high-precision Calculator tool to help, in addition, you can call in bash "awk" script to handle floating-point operations.
1. Use BC to process calculations (including integer and floating-point calculations)
Bc–an Arbitrary Precision Calculator language
(1). The example format used for using BC in bash scripts is:
variable=$ (echo "options; OPERATIONS "| BC [options]) that is: echo "[option]; action" | BC [Options]
(2). In the following script, it is mentioned that in the first option, the "scale" variable represents the precision of the output decimal points, and can be used to control the precision of the computed result; "IBase" and "obase" represent the input and output data respectively, and can be used in the conversion of the numerical system.
(3). A comparison of floating-point numbers, such as "if [$ (echo" $big > $small "| bc)-EQ 1]", passing a logical judgment to BC. If the result is true then output 1, otherwise output 0, then you can use this result for further operation.
(4). BC was originally used as input for the calculation of a file (there is also a demo), so you can write very complex calculations in the file, and then let the BC tool to deal with the results everywhere.
Note: When using the division operator/, to retain decimals, you need to set scale, otherwise the default scale, 0 digits after the decimal point.
2. Use awk to handle floating-point calculations and floating-point comparisons
Do not explain too much, wrote the sample script as follows, understand this will know how to deal with floating-point calculations and floating point comparison.
Copy Code code as follows:
#!/bin/bash
# Author:jay <smile665@gmail.com>
# Some examples for playing with floating point number.
# Basic usage of ' BC ' tool in Bash.
a=3.33
b=3.3
c=$ (echo "$a + $b" | BC)
d=$ (echo "$a * $b" | BC)
E=$ (echo "scale=5; $a/$b "| bc
echo "c=a+b= $a + $b = $c"
echo "d=a*b= $a * $b = $d"
echo "e=a/b= $a/$b = $e"
# "-L" parameter for ' BC ' means using the math library.
Pi=$ (echo "scale=10; 4*a (1) "| BC-L)
S=$ (echo "s ($PI/6)" | bc-l)
echo "Pi= $pi"
echo "S=sin (PI/6) = $s"
# Use more options of ' BC ' tool
r=$ (Echo ' ibase=10;obase=2; 15+16 ' | BC)
echo "Binary of (15+16) is $r"
# Comparison for floating point numbers using ' BC '
big=100
small=99.9
If [$ (echo "$big > $small" | bc)-EQ 1]; Then
echo "$big is bigger than $small"
Fi
# deal with floating-numbers with ' awk ' language
echo $ (awk-v x=10-v y=2.5 ' BEGIN {printf ' 10/2.5=%.2f\n ', x/y} ')
v=$ (echo $big $small | awk ' {printf '%0.8f\n ', $1/$2} ')
echo "$big/$small = $v"
echo $big $small | awk ' {if ($1>$2) {printf '%f >%f\n ', $1,$2} else {printf '%f <%f\n ', $1,$2}} '
The results of the implementation are as follows:
Copy Code code as follows:
master@jay-linux:~/workspace/mygit/shell/sh2012$./floating-point.sh
c=a+b=3.33+3.3=6.63
d=a*b=3.33*3.3=10.98
e=a/b=3.33/3.3=1.00909
pi=3.1415926532
S=sin (PI/6) =.49999999994373819220
Binary of (15+16) is 11111
is bigger than 99.9
10/2.5=4.00
100/99.9 = 1.00100100
100.000000 > 99.900000
In addition, the BC processes the computational logic in a file, which is illustrated as follows:
Copy Code code as follows:
master@jay-linux:~/workspace/mygit/shell/sh2012$ Cat TEMP.BC
3+8
3/8
scale=2; 3/8
master@jay-linux:~/workspace/mygit/shell/sh2012$ Bc-q TEMP.BC
11
0
.37
BC is a powerful tool, please "man BC" view the details; again, "Man awk."