Problems
in this case requires familiarity with the characteristics of Linux Shell environment, the main practice of the following:
use expr, $ [], let other integer arithmetic means: definition of variables X = 1234, then calculates X and four arithmetic operations and modulo result of 78
using bc achieved decimal arithmetic operations: calculating interactively 12.34 56.78 the results of the four operations, an additional non-interactively repeats the above calculations, as many as four decimal
steps
to achieve this case the following procedure is required.
Step a: integer arithmetic means
1) using the command expr
multiplication operator * should escape, to avoid as Shell wildcards; need to be separated by a space involved in computing the integer arithmetic operator, must be added $ symbol reference variable.
First, define the variable X = 1234, 78 then calculates the modulo arithmetic and the results are:
[root@svr5 ~]# X=1234 //定义变量X
[root@svr5 ~]# expr $X + 78 //加法
1312
[root@svr5 ~]# expr $X - 78 //减法
1156
[root@svr5 ~]# expr $X \* 78 //乘法,操作符应添加\转义
96252
[root@svr5 ~]# expr $X / 78 //除法,仅保留整除结果
15
[root@svr5 ~]# expr $X % 78 //求模
64
2) Use
(()) expression
multiplication without escaping * arithmetic operator can no space on both sides; symbol $ reference variable may be omitted; expression itself results Alternatively, the output may be combined echo command.
Also for variable X = 1234, and 78 were calculated and modulo arithmetic result:
[root@svr5 ~]# X=1234
[root@svr5 ~]# echo $[X+78]
1312
[root@svr5 ~]# echo $[X-78]
1156
[root@svr5 ~]# echo $[X*78]
96252
[root@svr5 ~]# echo $[X/78]
15
[root@svr5 ~]# echo $[X%78]
64
3) use the let command
expr or
(())Only operation mode, and does not change the value of the variable; and let command can be done directly save operation to the new value of the variable value. Thus variable X = 1234, the value obtained by performing operation will let the change; Further, let arithmetic operation result is not displayed, but can be viewed in conjunction with echo command:
[root@svr5 ~]# X=1234
[root@svr5 ~]# let X+=78 ; echo $X
1312
[root@svr5 ~]# let X-=78 ; echo $X
1234
[root@svr5 ~]# let X*=78 ; echo $X
96252
[root@svr5 ~]# let X/=78 ; echo $X
1234
[root@svr5 ~]# let X%=78 ; echo $X
64
Step two: fractional arithmetic means
1) bc interactive operation
before the bc command to enter the interactive environment, and then enter an expression to be calculated. To calculate a decimal arithmetic 5.678 12.34 and four for example, related as follows:
[root@svr5 ~]# bc
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
12.34+56.78 //加法
69.12
12.34-56.78 //减法
-44.44
12.34*56.78 //乘法
700.66
12.34/56.78 //除法
0
quit //退出交互计算器
[root@svr5 ~]#
2) bc noninteractive operation
you will need to bc arithmetic operation expression through line operation. Note that, the length of decimal places scale = N can be limited, except by the decimal also involved in the influence of numerical operations. To calculate a decimal arithmetic 5.678 12.34 and four for example, related as follows:
[root@svr5 ~]# echo 'scale=4;12.34+5.678' | bc
18.018
[root@svr5 ~]# echo 'scale=4;12.34-5.678' | bc
6.662
[root@svr5 ~]# echo 'scale=4;12.34*5.678' | bc
70.0665
[root@svr5 ~]# echo 'scale=4;12.34/5.678' | bc
2.1733