linux|shell programming|realization of addition, subtraction, multiplication and division in shell script and calculation example of π value

Foreword:

The addition, subtraction, multiplication, and division in the shell script is because when writing the kubernetes inspection script, some parts need to do some simple calculations, and I suddenly found that I am actually not familiar with these.

Therefore, I have consulted some information, and now I will make a simple summary of how to apply addition, subtraction, multiplication, and division operations in shell scripts. If you write something wrong, please tap it

one

Basic implementation of addition, subtraction, multiplication and division of shell scripts

First, let's look at a wrong demonstration, simply connect two integer variables with a + sign

#!/bin/bash
a=10
b=11

echo $a+$b

The output is as follows;

[root@EULER2 ~]# bash test.sh 
10+11

It can be seen that it is printed out as it is, which is not the result we want. Where does this problem occur?

It turns out that the shell treats the data (value or variable) on both sides of + as a string, and + as a string connector. The final result is to splice the two strings together to form a new string.

Therefore, we can use the form of $(()) to wrap the expression, but it should be noted that this method only supports integer digital operations

#!/bin/bash
a=10
b=11

echo $((a+b))

There is also an equivalent form of the let command:

#!/bin/bash
a=10
b=11
let c=a+b
echo $c

The following is a modified let operation expression (multiple expressions, separate expressions with spaces):

#!/bin/bash
a=10
b=11
let c=a+b d=a*c
echo $c $d

(Multiple expressions, separate expressions with commas)  , but only the last expression is output at this time

#!/bin/bash
a=10
b=11
echo $((a+b,a*b))

two,

Division in arithmetic expressions

Division is a special existence, no matter what programming language it is, division is a special existence, because the division cannot be zero, otherwise an error will be reported, and the second is integer division or floating-point precision division, that is to say, there are precision requirements for the result of division.

OK, then, let’s talk about integer division first, that is, when two numbers are divided, the result is only rounded

A:

Divisibility of division, no precision required

Example 1:

#### Note: Note that there must be spaces before and after the / sign

#!/bin/bash
a=18
b=2
c=`expr  $a / $b`
echo $c

####The following output is 7, the actual calculation result is 7.5, which is omitted

#!/bin/bash
a=15
b=2
c=`expr  $a / $b`
echo $c

OK, bc is required for the upper precision. Neither let nor expr can handle floating-point arithmetic, and if the precision of the result cannot be controlled, you can use the bc command instead. bc is generally used together with the pipe symbol |.

#### Note: The bc command is not installed to minimize, so centos7 probably needs yum install bc -y installation

B,

Non-integer divisibility of division, with precision requirements

Example 2:

#### Note, echo is followed by double quotes, single quotes are not acceptable

#!/bin/bash
a=10
b=3
c=`echo "scale=2;$a/$b" | bc`
echo $c

Example 3 (calculating the value of π in the shell):

####principle:

After adding the -l parameter to the bc command, you can use mathematical functions such as sin/cos/atan, among which:
a (x) The arctangent of x, arctangent returns radians. # is to calculate the arctangent of atan().
Since tan(π/4) = 1, π = 4*atan(1)

#####Note: The calculation of π value can reflect the computing power level of the CPU. The better the computer, the more digits of the calculated π value and the faster the speed. Therefore, the computing power of many supercomputers is also based on the calculation of this π value.

[root@EULER2 ~]# echo "scale=100; a(1)*4" | bc -l
3.141592653589793238462643383279502884197169399375105820974944592307\
8164062862089986280348253421170676

C,

The bc command inputs and outputs various bases

The bc tool is very powerful, with parameters obase and ibase can be converted into bases, this function can be easily realized through bc (the two parameters are in decimal by default, which is the Arabic numerals we usually recognize)

Example 4:

[root@node1 ~]# echo "ibase=10;obase=2; 4*6"|bc
11000
[root@node1 ~]# echo "ibase=10;obase=10; 4*6"|bc
24

#10进制转化成二进制
[root@node1 ~]# echo "ibase=10;obase=2; 22"|bc
10110

#十进制转化成十六进制
[root@node1 ~]# echo "obase=16;15" | bc
F

#十进制转化成八进制
[root@node1 ~]# echo "obase=8;15" | bc
17

#十进制转化成二进制
[root@node1 ~]# echo "obase=2;15" | bc
1111


#二进制转化成10进制
[root@node1 ~]# echo "ibase=2;1111" | bc
15

D,

square root calculation

### Note, the keyword sqrt, there is nothing to explain about this

[root@node1 ~]# echo "scale=6; sqrt(2)" | bc
1.414213
[root@node1 ~]# echo "scale=6; sqrt(4)" | bc
2.000000

Guess you like

Origin blog.csdn.net/alwaysbefine/article/details/131805955
Recommended