shell(五)运算符

简介: Shell的运算符比较有意思。为什么这么说呢?shell编程是一门偏底层的语言,他并不像我现在正在使用的PHP或者javascript这种解释语言这样,如下所示:bash复制代码[root@VM_0_4_centos test]# echo 1+21+2在控制台直接输入1+2,shell并不会帮我们计算出结果,而是把他当成字符串了,直接输出。这就需要使用到我们下边要看的算数运算符了 一:算数运算符需要使用到expr,expr 是一款表达式计算工具,使用它能完成表达式的求值操作。但是expr在使用的时候对于语法也是有要求的:

Shell的运算符比较有意思。为什么这么说呢?shell编程是一门偏底层的语言,他并不像我现在正在使用的PHP或者javascript这种解释语言这样,如下所示:

bash

复制代码

[root@VM_0_4_centos test]# echo 1+2
1+2

在控制台直接输入1+2,shell并不会帮我们计算出结果,而是把他当成字符串了,直接输出。

这就需要使用到我们下边要看的算数运算符了

一:算数运算符

需要使用到expr,expr 是一款表达式计算工具,使用它能完成表达式的求值操作。

但是expr在使用的时候对于语法也是有要求的:

bash

复制代码

expr 1+1
[root@VM_0_4_centos test]# expr 1+1
1+1

 

如上面的执行结果所示,这样写明显是不行的。应该这样写:

bash

复制代码

expr 1 + 1          
[root@VM_0_4_centos test]# expr 1 + 1
2

 

但是我觉得吧,这样还是不方便,有没有更方便一点的办法呢?你别说,还真有:使用$[],如下所示:

bash

复制代码

[root@VM_0_4_centos test]# result=$[2+3]
[root@VM_0_4_centos test]# echo $result
5
[root@VM_0_4_centos test]# result=$[2/3]
[root@VM_0_4_centos test]# echo $result
0
[root@VM_0_4_centos test]# result=$[2*3]
[root@VM_0_4_centos test]# echo $result
6

上方测试了 加、乘、除。下面我们测试的数据更复杂一点:

bash

复制代码

[root@VM_0_4_centos test]# result=$[2*(3+5)]
[root@VM_0_4_centos test]# echo $result
16

通过上边的测试,我们发现,只要在$[]中正常写算式就可以了。都能计算。.

 

最后放一下shell的常用运算符:

运算符 说明 举例
+ 加法 expr $a + $b 结果为 30。
- 减法 expr $a - $b 结果为 -10。
* 乘法 expr $a \* $b 结果为  200。
/ 除法 expr $b / $a 结果为 2。
% 取余 expr $b % $a 结果为 0。
= 赋值 a=$b 把变量 b 的值赋给 a。
== 相等。用于比较两个数字,相同则返回 true。 [ a==a == a==b ] 返回 false。
!= 不相等。用于比较两个数字,不相同则返回 true。 [ a!=a != a!=b ] 返回 true。

 

二:关系运算符

这部分在上篇条件判断语句中看过了,这里不再赘述。

 

三:布尔运算符

布尔运算符如下图所示:

运算符 说明 举例
! 非运算,表达式为 true 则返回 false,否则返回 true。 [ !   false ] 返回 true。
-o 或运算,有一个表达式为 true 则返回 true。 [   a−lt20−oa -lt 20 -o alt20ob -gt 100 ] 返回 true。
-a 与运算,两个表达式都为 true 才返回 true。 [   a−lt20−aa -lt 20 -a alt20ab -gt 100 ] 返回 false。

 

测试一下:

csharp

复制代码

# 定义两个变量
[root@VM_0_4_centos test]# a=10
[root@VM_0_4_centos test]# b=20
# 非运算符
[root@VM_0_4_centos test]# [ $a != $b ]
[root@VM_0_4_centos test]# echo $?
0
# 或运算符
[root@VM_0_4_centos test]# [ $a != $b -o $a == $b ]
[root@VM_0_4_centos test]# echo $?
0
# 与运算符
[root@VM_0_4_centos test]# [ $a != $b -a $a == $b ]
[root@VM_0_4_centos test]# echo $?
1

 

四:逻辑运算符

Shell中的逻辑运算符如下表所示:

运算符 说明 举例
&& 逻辑的 AND [[   a -lt 100 && b -gt 100 ]] 返回 false
逻辑的 OR [[   $a -lt 100 $b -gt 100 ]] 返回 true

测试一下:

ini

复制代码

# 声明两个变量
[root@VM_0_4_centos test]# a=10
[root@VM_0_4_centos test]# b=20
# 这是一个错误的示范,这样写会报错的
[root@VM_0_4_centos test]# [ $a == $b && $a != $b ]
-bash: [: missing `]'
# 正确的语法是两个中括号
[root@VM_0_4_centos test]# [[ $a == $b && $a != $b ]]
[root@VM_0_4_centos test]# echo $?
1
# 逻辑and
[root@VM_0_4_centos test]# [[ $a != 30 && $a != $b ]]
[root@VM_0_4_centos test]# echo $?
0
# 逻辑 or
[root@VM_0_4_centos test]# [[ $a == $b || $a != $b ]]
[root@VM_0_4_centos test]# echo $?
0

五:字符串运算符

Shell中的字符串运算符如下表所示:

运算符 说明 举例
= 检测两个字符串是否相等,相等返回   true。 [   a=a = a=b ] 返回 false。
!= 检测两个字符串是否不相等,不相等返回   true。 [   a!=a != a!=b ] 返回 true。
-z 检测字符串长度是否为0,为0返回 true。 [   -z $a ] 返回 false。
-n 检测字符串长度是否不为 0,不为 0 返回 true。 [   -n "$a" ] 返回 true。
$ 检测字符串是否不为空,不为空返回   true。 [   $a ] 返回 true。

测试一下:

csharp

复制代码

# 定义两个变量
[root@VM_0_4_centos test]# a=hello
[root@VM_0_4_centos test]# b=world
# 判断是否相等,注意shell中判断是否等于 使用 =
[root@VM_0_4_centos test]# [ $a = $b ]
[root@VM_0_4_centos test]# echo $?
1
# 判断是否不相等
[root@VM_0_4_centos test]# [ $a != $b ]
[root@VM_0_4_centos test]# echo $?
0
# 检测字符串长度是否为0
[root@VM_0_4_centos test]# [ -z $b ]
[root@VM_0_4_centos test]# echo $?
1
# 检测字符串长度是否不为0
[root@VM_0_4_centos test]# [ -n $b ]
[root@VM_0_4_centos test]# echo $?
0
# 检测字符串是否不为空
[root@VM_0_4_centos test]# [ $b ]
[root@VM_0_4_centos test]# echo $?
0

 

六:文件测试运算符

Shell中的文件测试运算符如下表所示:

操作符 说明 举例
-b file 检测文件是否是块设备文件,如果是,则返回 true。 [ -b $file ] 返回 false。
-c file 检测文件是否是字符设备文件,如果是,则返回 true。 [ -c $file ] 返回 false。
-d file 检测文件是否是目录,如果是,则返回 true。 [ -d $file ] 返回 false。
-f file 检测文件是否是普通文件(既不是目录,也不是设备文件),如果是,则返回 true。 [ -f $file ] 返回 true。
-g file 检测文件是否设置了 SGID 位,如果是,则返回 true。 [ -g $file ] 返回 false。
-k file 检测文件是否设置了粘着位(Sticky Bit),如果是,则返回 true。 [ -k $file ] 返回 false。
-p file 检测文件是否是有名管道,如果是,则返回 true。 [ -p $file ] 返回 false。
-u file 检测文件是否设置了 SUID 位,如果是,则返回 true。 [ -u $file ] 返回 false。
-r file 检测文件是否可读,如果是,则返回 true。 [ -r $file ] 返回 true。
-w file 检测文件是否可写,如果是,则返回 true。 [ -w $file ] 返回 true。
-x file 检测文件是否可执行,如果是,则返回 true。 [ -x $file ] 返回 true。
-s file 检测文件是否为空(文件大小是否大于0),不为空返回 true。 [ -s $file ] 返回 true。
-e file 检测文件(包括目录)是否存在,如果是,则返回 true。 [ -e $file ] 返回 true。

这里就不做测试了,用法比较简单。

有好的建议,请在下方输入你的评论。

目录
相关文章
|
人工智能 机器人 Shell
【shell】shell条件判断、循环语句、基本运算符
【shell】shell条件判断、循环语句、基本运算符
|
Shell Linux
Linux下的Shell基础——变量、运算符、条件判断(二)
Linux下的Shell基础——变量、运算符、条件判断(二)
234 0
|
Linux Shell
|
Linux Shell
|
Java Unix Shell
Shell 基本运算符
10月更文挑战第3天
94 0
|
运维 Shell C语言
第三章 Shell表达式与运算符
第三章 Shell表达式与运算符
|
Unix Linux Shell
|
Linux Shell
|
Linux Shell