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