网站开发技术

记点笔记、 学点技术 欢迎交流建站技术。本站关注lamp技术

您尚未登录。

#1 2014-12-24 11:38:31

admin
管理员

shell 中的是数学计算

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

离线

页脚

Powered by FluxBB