1. 程式人生 > >數據運算,條件測試,if選擇結構

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

條件測試 整數 ech 數據運算 操作 ane ast 命令 oot

Shell中的數值運算
1、整數運算工具
一。使用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 //求模
64

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

[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]
64

三。使用let命令

[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)
64

未完,待續

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