Linux Shell 數學運算

Linux Shell 數學運算

    在Linux中直接使用數學運算符進行數學運算每每得不到咱們想要的計算結果。要在Shell中進行數學運算,咱們須要藉助點小手段。目前,Linux Shell中進行數學運算的方法主要有三種:bc、expr、let。shell

1 bc

1.1 命令行方式

    在bash界面,直接輸入bc或者bc -q,就能夠進去bc的命令行,經過使用數學運算符可以獲得咱們想要的結果:vim

 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,能夠定義當前的小數點精度,對除法、取餘和冪運算有效。centos

這種方式只能在bc的命令行中進行,在代碼中固然不能這樣幹了。bash

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中應用的更多一些,一樣能夠在運算的時候加上精度的限制。spa

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的內容爲:code

1+2

1-2

2*3

2/3

scale=2;3/4

scale=0;3/4

 

就是表達式的集合。blog

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
相關文章
相關標籤/搜索