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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数据运算,条件测试,if选择结构相关的知识,希望对你有一定的参考价值。

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

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

[[email protected] ~]# x=1234
[[email protected] ~]# echo $[x+78]
1312
[[email protected] ~]# echo $[x-78]
1156
[[email protected] ~]# echo $[x*78]
96252
[[email protected] ~]# echo $[x/78]
15
[[email protected] ~]# echo $[x%78]
64

三。使用let命令

[[email protected] ~]# x=1234
[[email protected] ~]# let y=x+22
[[email protected] ~]# echo $y
1256
[[email protected] ~]# let x+=78;echo $x # x+=78(x=x+78)
1312
[[email protected] ~]# let x-=78;echo $x # x-=78(x=x-78)
1234
[[email protected] ~]# let x=78;echo $x # x=78(x=x*78)
96252
[[email protected] ~]# let x/=78;echo $x # x/=78(x=x/78)
1234
[[email protected] ~]# let x%=78; echo $x # x%=78(x=x%79)
64

未完,待续

以上是关于数据运算,条件测试,if选择结构的主要内容,如果未能解决你的问题,请参考以下文章

2Python程序控制结构(0530)

bash中选择结构循环结构与breakcontinue

shell脚本之条件语句 与 if 语句

运算符和选择结构

Shell条件测试)

if语句 条件测试 shell编程之条件语句