页次: 1
linux shell 中的数学计算可以用 expr 和 bc
首先看下例子
[root@ippbs ~]# expr 1+1 1+1 [root@ippbs ~]# expr 1 + 1 2 [root@ippbs ~]# a=2 [root@ippbs ~]# b=4 [root@ippbs ~]# expr $a+$b 2+4 [root@ippbs ~]# expr $a + $b 6
!!!说明:变量和运算符要有空格
[root@ippbs ~]# expr $a * $b expr: syntax error [root@ippbs ~]# expr $a \* $b 8
!!! 乘法要转义
支持加减乘除
[root@ippbs ~]# expr 1 + 2 3 [root@ippbs ~]# expr 2 \* 3 6 [root@ippbs ~]# expr 5 % 3 2 [root@ippbs ~]# expr 5 / 3 1
支持大小比较
[root@ippbs ~]# expr 1 \>= 2 0 [root@ippbs ~]# expr 1 \<= 2 1 [root@ippbs ~]# expr 1 != 2 1 [root@ippbs ~]# expr 1 = 2 0 [root@ippbs ~]# expr 1 \< 2 1
或与
[root@ippbs ~]# expr 1 \| 2 1 [root@ippbs ~]# expr 0 \| 2 2 [root@ippbs ~]# expr 100 \| 2 100
与
[root@ippbs ~]# expr 100 \& 2 100 [root@ippbs ~]# expr 0 \& 2 0
ipbbs.net
离线
页次: 1