Linux Shell 数学运算

Linux Shell 数学运算

    在Linux中直接使用数学运算符进行数学运算往往得不到我们想要的计算结果。要在Shell中进行数学运算,我们需要借助点小手段。目前,Linux Shell中进行数学运算的方法主要有三种:bc、expr、let。

1 bc

1.1 命令行方式

    在bash界面,直接输入bc或者bc -q,就可以进去bc的命令行,通过使用数学运算符能够得到我们想要的结果:

 1 [scott@centos1 ~]$ bc -q
 2 
 3 2+4
 4 
 5 6
 6 
 7 2-4
 8 
 9 -2
10 
11 2*4
12 
13 8
14 
15 2/4
16 
17 0
18 
19 2%4
20 
21 2
22 
23 2^4
24 
25 16
26 
27 scale=2;2/4
28 
29 .50
30 
31 2%4
32 
33 0
34 
35 scale=0;2/4
36 
37 0
38 
39 2%4
40 
41 2

输入运算数和运算符号,回车即可得到运算结果。通过设置scale,可以定义当前的小数点精度,对除法、取余和幂运算有效。

这种方式只能在bc的命令行中进行,在代码中当然不能这样干了。

1.2 管道方式

 1 [scott@centos1 ~]$ echo 2+3|bc
 2 
 3 5
 4 
 5 [scott@centos1 ~]$ echo 2-3|bc
 6 
 7 -1
 8 
 9 [scott@centos1 ~]$ echo 2*3|bc
10 
11 6
12 
13 [scott@centos1 ~]$ echo 2/3|bc
14 
15 0
16 
17 [scott@centos1 ~]$ echo 2%3|bc
18 
19 2
20 
21 [scott@centos1 ~]$ echo "scale=2;2/3"|bc
22 
23 .66
24 
25 [scott@centos1 ~]$ echo "scale=2;2%3"|bc
26 
27 .02
28 
29 [scott@centos1 ~]$ echo "scale=2;3/2"|bc
30 
31 1.50
32 
33 [scott@centos1 ~]$ echo "scale=2;3%2"|bc
34 
35 0
36 
37 [scott@centos1 ~]$ echo 2^3|bc
38 
39 8

    这种管道方式在shell中应用的更多一些,同样可以在运算的时候加上精度的限制。

1.3 进制转换

 1 [scott@centos1 ~]$ echo "ibase=10;15"|bc
 2 
 3 15
 4 
 5 [scott@centos1 ~]$ echo "ibase=8;15"|bc
 6 
 7 13
 8 
 9 [scott@centos1 ~]$ echo "ibase=16;F"|bc
10 
11 15

    上文的例子,是把几种进制都转化为10进制。

1.4 表达式运算

 1 [scott@centos1 ~]$ vim bc-test.bc
 2 
 3 [scott@centos1 ~]$ bc -q bc-test.bc
 4 
 5 3
 6 
 7 -1
 8 
 9 6
10 
11 0
12 
13 .75
14 
15 0

    其中,bc-test.bc的内容为:

1+2

1-2

2*3

2/3

scale=2;3/4

scale=0;3/4

就是表达式的集合。

2 expr

    expr是个很强大的命令,可以进行数学运算,也可以进行字符串的操作等。先看下数学运算的功能。

 1 [scott@centos1 ~]$ expr 3+4
 2 
 3 3+4
 4 
 5 [scott@centos1 ~]$ expr 3 +4
 6 
 7 expr: syntax error
 8 
 9 [scott@centos1 ~]$ expr 3 + 4
10 
11 7
12 
13 [scott@centos1 ~]$ expr 3 * 4
14 
15 expr: syntax error
16 
17 [scott@centos1 ~]$ expr 3 * 4
18 
19 12
20 
21 [scott@centos1 ~]$ expr 3 / 4
22 
23 0
24 
25 [scott@centos1 ~]$ expr 3 % 4
26 
27 3

    expr不支持浮点运算,不支持幂乘运算,在运算的时候可要注意运算符和运算数的分离,写在一起可是不识别的,另外,乘法有点特殊,需要转义。

    下面看看expr的字符串操作。

 1 [scott@centos1 ~]$ string=123456789asdfg
 2 
 3 [scott@centos1 ~]$ expr length $string
 4 
 5 14
 6 
 7 [scott@centos1 ~]$ expr index $string '456'
 8 
 9 4
10 
11 [scott@centos1 ~]$ expr substr $string 7 4
12 
13 789a
14 
15 [scott@centos1 ~]$ expr substr $string 7 11
16 
17 789asdfg

    上例分别利用expr命令进行了计算字符串长度、获取字串或者字符的首次出现位置、取指定位置开始的限定长度的字符字串,需要注意的是expr中的下标是从1开始的。

3 let

 1 [scott@centos1 ~]$ let a=2+3
 2 
 3 [scott@centos1 ~]$ echo $a
 4 
 5 5
 6 
 7 [scott@centos1 ~]$ let a=2*3
 8 
 9 [scott@centos1 ~]$ echo $a
10 
11 6
12 
13 [scott@centos1 ~]$ let a=2/3
14 
15 [scott@centos1 ~]$ echo $a
16 
17 0
18 
19 [scott@centos1 ~]$ let a=2%3
20 
21 [scott@centos1 ~]$ echo $a
22 
23 2
24 
25 [scott@centos1 ~]$ let a=2^3
26 
27 [scott@centos1 ~]$ echo $a
28 
29 1
30 
31 [scott@centos1 ~]$ let a=2**3
32 
33 [scott@centos1 ~]$ echo $a
34 
35 8

    需要注意的是,let命令里的幂乘运算不是^,而是**。

4 其他方式

 1 [scott@centos1 ~]$ echo $((3+5))
 2 
 3 8
 4 
 5 [scott@centos1 ~]$ echo $((3*5))
 6 
 7 15
 8 
 9 [scott@centos1 ~]$ echo $((3**5))
10 
11 243
12 
13 [scott@centos1 ~]$ echo $((((3+5))*3))
14 
15 24
16 
17 [scott@centos1 ~]$ echo `date`
18 
19 Fri Aug 16 08:24:33 PDT 2013
20 
21 [scott@centos1 ~]$ echo `date +%Y%m%d`
22 
23 20130816
原文地址:https://www.cnblogs.com/Scott007/p/3263691.html