數據運算,條件測試,if選擇結構

Shell中的數值運算
一、整數運算工具
一。使用expr工具
Last login: Sun Feb 11 21:08:31 on console
Thegod:~ chengde$ x=1234 //定義變量x
Thegod:~ chengde$ expr $x + 78 //加法
1312
Thegod:~ chengde$ expr $x - 78 //減法
1156
Thegod:~ chengde$ expr $x * 78 //乘法,操做符應添加\轉義
96252
Thegod:~ chengde$ expr $x / 78 //除法,僅保留整除結果
15
Thegod:~ chengde$ expr $x % 78 //求模
64ide

二。使用$[ ]或$(())表達式工具

[root@pangdanet ~]# x=1234
[root@pangdanet ~]# echo $[x+78]
1312
[root@pangdanet ~]# echo $[x-78]
1156
[root@pangdanet ~]# echo $[x*78]
96252
[root@pangdanet ~]# echo $[x/78]
15
[root@pangdanet ~]# echo $[x%78]
64it

三。使用let命令console

[root@2015idc ~]# x=1234
[root@2015idc ~]# let y=x+22
[root@2015idc ~]# echo $y
1256
[root@2015idc ~]# let x+=78;echo $x # x+=78(x=x+78)
1312
[root@2015idc ~]# let x-=78;echo $x # x-=78(x=x-78)
1234
[root@2015idc ~]# let x=78;echo $x # x=78(x=x*78)
96252
[root@2015idc ~]# let x/=78;echo $x # x/=78(x=x/78)
1234
[root@2015idc ~]# let x%=78; echo $x # x%=78(x=x%79)
64ast

未完,待續class

相關文章
相關標籤/搜索