码迷,mamicode.com
首页 > 其他好文 > 详细

数据运算,条件测试,if选择结构

时间:2018-02-11 22:41:51      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:变量   $[ ]   表达式   测试   ons   加法   使用   定义变量   echo   

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选择结构

标签:变量   $[ ]   表达式   测试   ons   加法   使用   定义变量   echo   

原文地址:http://blog.51cto.com/20214843/2071215

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!