当前位置:首页 > 编程笔记 > 正文
已解决

shell条件测试与条件测试操作符

来自网友在路上 159859提问 提问时间:2023-10-22 00:31:07阅读次数: 59

最佳答案 问答题库598位专家为你答疑解惑

shell条件测试与条件测试操作符

      • 条件测试
      • 变量#?
      • test与条件测试语句
      • 方括号测试表达式
      • 字符串测试操作符
      • 逻辑测试操作符
      • 整数测试操作符
      • 双小括号的整数测试操作符与含义
      • 文件测试操作符
      • 条件测试举例

条件测试

条件测试是可以根据某个特定条件是否满足,来选择执行相应的任务。

变量#?

任何一种测试中,都要有退出状态(返回值),退出状态为0表示命令成功或表达式为真,非0则表示命令失败或表达式为假。状态变量S?中保存了命令退出状态的值,

查看用户是否在/etc/passwd中,0表示真,存在,1表示假,不存在

[root@localhost tmp]# grep $USER /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@localhost tmp]# echo $?
0
[root@localhost tmp]# grep josh /etc/passwd;echo $?
1

test与条件测试语句

test 命令在 shell 中主要用于条件测试,如果条件为真,则返回一个0值。如果表达式不为真,则返回一个大于 0的值,也可以将其称为假值。
test 命令支持测试的范围包括:字符串比较、算术比较、文件是否存在、文件属性和类型判断等。例如,判断文件是否为空、文件是否存在、是否是目录、变量是否大于3、字符串是否等于ivey、字符串是否为空等。

[root@localhost tmp]# x=8;y=12
[root@localhost tmp]# test $x -gt $y 	#gt表示大于
[root@localhost tmp]# echo $?
1[root@localhost tmp]# test $x -lt $y	#lt表示小于
[root@localhost tmp]# echo $?
0

方括号测试表达式

方括号代替test,方括号前后都留空格

[root@localhost tmp]# x=8;y=12
[root@localhost tmp]# [ $x -gt $y ];echo $?
1
[root@localhost tmp]# [ $x -lt $y ];echo $?  
0

字符串测试操作符

字符串测试操作符的作用有:比较两个字符串是否相同、字符串的长度是否为零、字符串是否为NULL等。

字符串操作符				含义
[ -z str ]				如果字符串str长度为0,返回真
[-n str ]				如果字符串 str 长度不为 0,返回真
[ str ]					如果字符串str 不为空,返回真
[ strl = str2 ]			测试字符串str]是否与字符串str2相等,可使用--代替-
[ str1 != str2 ]		测试字符串str1是否与字符串str2不相等,但不能用!-代替!
[[ strl -- str2 ]]		两字符串相同返回真
[[ strl != str2 ]]		两字符串不相同返回真
[[ strl -~ str2 ]]		str2是strl的子串返回真
[[ strl > str2 ]]		str1大于str2返回真
[[ strl < str2 ]]		str1小于st2返回真

单、双中括号操作符两边必须留空格!字符串大小比较是按从左到右对应字符的ASCII码进行比较。

例子1,判读字符串$name长度是否为0,不为0返回1

[root@localhost tmp]# name1=josh; [ -z $name ]; echo $?
1

判断name1是否等于name2

[root@localhost tmp]# name2=andy; [ $name = $name2 ] ; echo $?
1

逻辑测试操作符

[ exprl -a expr2 ]	逻辑与,都为真时,结果为真
[ exprl -0 expr2 ]	逻辑或,有一个为真时,结果为真
[!expr]				逻辑非匹配模式的逻辑测试操作符,需要写在双中括号中
&&					逻辑与
||					逻辑或
!					逻辑非

注意在逻辑测试中,-a、-0 的含义,一个是逻辑与,一个是逻辑或。

[root@localhost tmp]# [ $x -eq 1 -a -n $name ]; echo $?
0
[root@localhost tmp]# a=10;b=11
[root@localhost tmp]# [[ $b -gt $a ]];echo $?			#判断b>a返回0
0

整数测试操作符

测试操作符               含义
[ intl -eq int2 ]        intl等于int2
[ intl -ne int2]         intl 不等于int2
[ intl -gt int2 ]        intl 大于int2
[ intl -ge int2]         int1大于或等于int2
[ intl -It int2 ]        intl小于int2
[intl -le int2]          int1小于或等于int2[[ intI -eq int2 ]]       intl等于int2返回真
[[ int1 -ne int2 ]]       intl不等于int2返回真
[[ intl -gt int2 ]]       int]大于int2返回真
[[ intl -ge int2 ]]       int1大于或等于int2返回真
[[ intl -It int2 ]]       int]小于int2返回真
[[ intl -le int2 ]]       int1小于或等于int2返回真

判断变量x是否等于1

[root@localhost ~]# x=1; [ $x -eq 1 ]; echo $?
0

y为字符串,将eq用于字符串与整数的比较时错误的,返回结果为假

[root@localhost ~]# y=a; [ $y -eq 1 ]; echo $?
-bash: [: a: 期待整数表达式
2

双小括号的整数测试操作符与含义

测试操作符					含义
((intl == int2)             intI等于int2返回真
((int1 != int2))            intl不等于int2返回真
((int1 > int2)              intl大于int2返回真
((int1 >= int2))            int1大于或等于int2返回真
((intl <int2)               intl小于int2返回真
(int1 <= int2))             int1小于或等于int2返回真

双小括号中整数测试操作符使用了=-、!=、>、>=、<、<= 等操作符,这些操作符只能用于整数测试,注意与单、双中括号中使用的操作符不同。另外,双小括号操作符两边的空格可省略。

[root@localhost ~]# a=2;b=6
[root@localhost ~]# [[ $a != $b ]]; echo $?
0[root@localhost ~]# [[ $a>  $b ]]; echo $? 
1

文件测试操作符

文件测试操作符用来测试文件是否存在、文件属性、访问权限等场景

测试操作符			含义
-f fhame            fname存在且是普通文件时,返回真(即返回0
-L fname            fname存在且是链接文件时,返回真
-d fname            fname存在且是一个目录时,返回真
-e fname            finame(文件或目录)存在时,返回真
-s fame             finame存在且大小大于0时,返回真
-r fname            finame(文件或目录)存在且可读时,返回真
-w fname            fname(文件或目录)存在且可写时,返回真
-X fname            fname(文件或目录)存在且可执行时,返回真

shellfile 是新创建的-个普通文件,返回真,不是则返回假

[root@localhost ~]# touch shellfile
[root@localhost ~]# ll  shellfile 
-rw-r--r--. 1 root root 0 1021 22:09 shellfile
[root@localhost ~]# [ -f shellfile ]; echo $?
0

shellfile不是一个目录,所以返回假

[root@localhost ~]# [ -d shellfilel ]; echo $?
1

#shellfile是一个存在的文件,所以返回真

[root@localhost ~]# [ -e shellfile ]; echo $?
0

条件测试举例

逻辑与测试

[root@localhost ~]# x=6; name=josh;
[root@localhost ~]# [ $x -eq 6 -a -n $name ]; echo $?
0
查看全文

99%的人还看了

猜你感兴趣

版权申明

本文"shell条件测试与条件测试操作符":http://eshow365.cn/6-21153-0.html 内容来自互联网,请自行判断内容的正确性。如有侵权请联系我们,立即删除!