12.shell常用运算符

算数符运算是指加,减,乘,除,余等常见运算,值得注意的是shell只支持整数计算,也就是所有可能产生小数的运算都会舍去小数部分。

优先级

运算符

功能描述

1

+      -

正、负

2

*    /      %

乘、除、取余

3

+      -

加、减

4

+=    -=      …

自增、自减   …

例1:加减乘除

[root@localhost ~]# aa=$(( (11+3)*3/2 ))

[root@localhost ~]# echo $aa

21

#虽然乘除的优先级高于加,但是通过小括号可以调整运算优先级。

变量的测试与内容置换:

变量置换方式

变量y没有设置

变量y为空值

变量y设置值

x=${y-新值}

x=新值

x为空

x=$y

x=${y+新值}

x为空

x=新值

x=新值

x=${y=新值}

x=新值

y=新值

x为空

y值不变

x=$y

y值不变

例:

变量y没有设置值时,x=新值

[root@localhost ~]# unset y 
[root@localhost ~]# x=${y-new} 
[root@localhost ~]# echo $x 
new 
[root@localhost ~]# echo $y 
[root@localhost ~]# 

变量y为空时,x为空

[root@localhost ~]# y="" 
[root@localhost ~]# x=${y-new} 
[root@localhost ~]# echo $x 
[root@localhost ~]# 

变量y为aa时,x=$y

[root@localhost ~]# y=aa 
[root@localhost ~]# x=${y-new} 
[root@localhost ~]# echo $xaa 

猜你喜欢

转载自blog.csdn.net/weixin_46659843/article/details/123789110