SHELL script strategy (Study Notes)-1.6 mathematical operations and bc commands, shell-1.6
Directory:
1.6.1 basic Integer Operation
1.6.2 bc command advanced arithmetic operation
Use let, $ (), or $ [] to perform basic integer operations. Use bc for advanced operations, including decimal operations. The expr command can also perform integer operations and determine whether the parameter is an integer. For details, see the full solution of the expr command.
1.6.1 basic Integer Operation
[Root @ xuexi tmp] # str = 10 [root @ xuexi tmp] # let str = str + 6 # equivalent to let str + = 6 [root @ xuexi tmp] # let str -= 5 # equivalent to let str = str-5 [root @ xuexi tmp] # echo $ str11
If you want to perform computation in the command line, you can use $ () or $ [].
[root@xuexi ~]# str=10[root@xuexi ~]# echo $((str+=6))16[root@xuexi ~]# echo $[str=str-6]10
Of course, you can use $ () and $ [] when assigning arithmetic values to variables.
[root@xuexi ~]# str=10[root@xuexi ~]# str=$((str+=6));echo $str16[root@xuexi ~]# str=$[str-=6];echo $str10
In fact, during arithmetic calculation, the variable on the right of the equal sign can carry the $ symbol, but the variable on the left of the equal sign cannot carry the $ symbol, because it is the variable to be operated, it is not a reference variable. For example:
[Root @ xuexi ~] # Let str = $ str-1 # equivalent to let str = str-1 [root @ xuexi ~] # Str = $ ($ str-1) # equivalent to str = $ (str-1) [root @ xuexi ~] # Srt = $ [$ str-1] # equivalent to str = $ [str-1] [root @ xuexi ~] # Echo $ (str = $ str-1) # equivalent to echo $ (str = str-1), but cannot be written as echo $ ($ str = str-1 )) [root @ xuexi ~] # Echo $ [str = $ str-1] # equivalent to echo $ [str = str-1], but not echo $ [$ str = str-1]
You can also perform auto-increment and auto-subtraction operations. "++" And "--" indicate that the variable is automatically incremented by 1 and subtracted by 1. However, the returned results vary depending on the location.
X ++: returns the result first, and Adds 1
++ X: Add 1 first and then return the result.
X --: returns the result first and then minus 1.
-- X: First minus 1 and then return the result
If the initial value of x is 10, echo $ [x ++] will display 10, but after the display is complete (that is, after the result is returned), the value of x has changed to 11, then execute echo $ x to return 11.
[root@xuexi ~]# x=10;echo $((x++));echo $x1011
If echo $ [x ++] returns 11 again, But x is already 12.
[root@xuexi ~]# echo $((x++));echo $x1112
When the initial value of the x variable is 10, the echo $ [++ x] will display 11, because the value of 1 is first added and then assigned to x, and the echo will display the value of x. ++ X is equivalent to x = x + 1. They all add 1 first and then assign values.
[root@xuexi ~]# x=10;echo $((++x));echo $x1111
Likewise, auto-Subtraction is the same.
Therefore, when using auto-increment or auto-increment to assign values to variables, pay attention to whether the value is effective immediately after addition or subtraction. For example:
[root@xuexi ~]# x=10;y=$((x++));echo $y;echo $y1010
Because the value assigned by y = $ (x ++) to y is the value before 1, although after the assignment is complete, $ (x ++) has become 11, but this has nothing to do with y.
Therefore, you should use the "++ x" or "-- x" method to assign values to variables of the auto-increment and auto-increment classes.
[root@xuexi ~]# x=10;y=$((++x));echo $y;echo $y1111
The following describes how to assign values to numeric variables:
let i=i-1
let i=$i-1
let i-=1
i=$((i-1))
i=$(($i-1))
i=$[ i - 1 ]
i=$[ $i - 1 ]
echo $((i=i-1))
echo $((i=$i-1))
1.6.2 bc command advanced arithmetic operation
Bc can be used to calculate floating point numbers. It is a calculator in linux.
The following is a basic function example:
[Root @ node1 ~] # Bcb 1.06.95 # first, output the bc version information. You can use the-q option to not output the header information Copyright 1991-1994,197,199 8, 2000,200 4, 2006 Free Software Foundation, Inc. this is free software with absolutely no warranty. for details type 'warranty '. pie = 3.1415 # variable assignment pie * 3*3 # space 28.2735r = 3pie * r * r28.2735pie * r ^ 2 # Power 28.2735r = 3 /* set the radius to 3 */#. You can also use C-language annotations.
Enter the quit command to exit the bc calculator.
The auto-increment and auto-increment functions are also supported.
[root@node1 ~]# bc -qr=3r++3r++4++r6++r7--r6
The bc splitter has a built-in variable scale, which is used to represent the computing precision. The default precision is 0, so the default result of division is an integer.
13/(1+3)3scale=313/(1+3)3.250
A more user-friendly function is to use command replacement to calculate the batch processing mode.
Its general format is as follows:
var=`echo "option1;option2;...;expression"|bc`
In the options section, the precision scale and variable assignment are generally set, and the expression section is the calculation expression. Finally, they are placed in the back quotes and assigned to the variable. For example:
[root@node1 ~]# area=`echo "scale=2;r=3;3.1415*r*r"|bc`[root@xuexi ~]# echo $area28.2735
Because it is specified in the command line, there are many restrictions on using this method. Bc accepts parameters using the here string and here document methods. The most common practice is to place them in scripts.
#! /Bin/bash # script for calculate something var1 = hahavar2 = hehe value = 'bc <EOF # Use the here string method in back quotes scale = 3r = 33.1415 * r * rEOF 'echo $ value
The following is the calculation of 1 + 2 +... the output results on the screen must be "1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 = calculation results ", this is a very good example.
[root@node1 tmp]# echo $(seq -s "+" 10)=`seq -s "+" 10|bc`1+2+3+4+5+6+7+8+9+10=55
[root@node1 tmp]# echo $(seq -s "+" 10)=$((`seq -s "+" 10`))1+2+3+4+5+6+7+8+9+10=55
[Root @ node1 tmp] # echo $ (seq-s "+" 10) = $ (seq-s "+" 10 | xargs expr) # Note: "+" and "+" 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 = 55
Back to series article outline: http://www.cnblogs.com/f-ck-need-u/p/7048359.html
Reprinted please indicate the source: http://www.cnblogs.com/f-ck-need-u/p/7231870.html