Shell-判断表达式

#test

测试的标志代表意义
1. 关于某个文件名的“文件类型”判断,如test -e filename表示存在否
-e该“文件名”是否存在? (常用)
-f该“文件名”是否存在且为文件(file) ? (常用)
-d该“文件名”是否存在且为目录(directory) ? (常用)

示例:

[root@localhost ~]# touch a.txt
[root@localhost ~]# test -e a.txt;echo $?
0                               # 测试成功,命令返回值为 0

[root@localhost ~]# test -e b.txt;echo $?
1                               # 测试失败,命令返回值为 非 0

[root@localhost ~]# test -f a.txt;echo $?
0

[root@localhost ~]# test -d a.txt;echo $?
1

测试的标志代表意义
2. 关于文件的权限侦测,如test -r filename表示可读否(但root权限常有例外)
-r侦测该文件名是否存在且具有“可读”的权限?
-w侦测该文件名是否存在且具有“可写”的权限?
-x侦测该文件名是否存在且具有“可执行”的权限?
-u侦测该文件名是否存在且具有“SUID”的属性?
-g侦测该文件名是否存在且具有“SGID”的属性?
-k侦测该文件名是否存在且具有“Sticky bit"的属性?
-s侦测该文件名是否存在且为“非空白文件”?

示例:

[root@localhost ~]# test -r a.txt; echo $?
0

[root@localhost ~]# test -x a.txt; echo $?
1

[root@localhost ~]# test -w a.txt; echo $?
0

[root@localhost ~]# test -u a.txt; echo $?           # 判断 a.txt 文件是否具有 SUID 属性
1

[root@localhost ~]# cat a.txt                        # 查看 a.txt ,此文件内容为空

[root@localhost ~]# test -s a.txt; echo $?           # 判断 a.txt 文件中有内容
1                                                    # 命令返回值为 1 ,说明文件中没有内容

[root@localhost ~]# echo "123" > a.txt

[root@localhost ~]# test -s a.txt; echo $?
0

测试的标志代表意义
3. 两个文件之间的比较,如:test file1 -nt file2
-nt(newer than) 判断 file1是否比 file2 新
-ot(older than) 判断 file1 是否比 file2 旧
-ef判断 file1与 file2 是否为同一文件,可用在判断 hard link 的判定上,主要意义在判定,两个文件是否均指向同一个inode里

示例:

[root@localhost ~]# touch b.txt

[root@localhost ~]# ls -l a.txt
-rw-r--r--. 1 root root 0 2月  25 21:08 a.txt

[root@localhost ~]# ls -l b.txt
-rw-r--r--. 1 root root 0 2月  25 21:10 b.txt

[root@localhost ~]# test a.txt -nt b.txt; echo $?         # 判断 a.txt 是否比 b.txt 新
1                                                         # 返回 1, 表示判断表达式不成立

[root@localhost ~]# test b.txt -nt a.txt; echo $?
0

[root@localhost ~]# ll -i a.txt b.txt
8409154 -rw-r--r--. 1 root root 0 2月  25 21:08 a.txt
8409154 -rw-r--r--. 1 root root 0 2月  25 21:10 b.txt

[root@localhost ~]# test a.txt -ef a-hard.txt; echo $?           #判断是否为同一文件
0                                                                #返回 0,表示为同一文件

测试的标志代表意义
4. 关于两个整数之间的判定,例如 test n1 -eq n2
-eq两数值相等 (equal)
-ne两数值不等 (not equal)
-gtn1 大于 n2 (greater than)
-Itn1 小于 n2 (less than)
-gen1 大于等于 n2 (greater than or equal)
-len1 小于等于 n2 (less than or equal)

示例:

[root@localhost ~]# test 10 -eq 20; echo $?
1

[root@localhost ~]# n1=10

[root@localhost ~]# n2=20

[root@localhost ~]# test $n1 -eq $n2; echo $?
1

[root@localhost ~]# test $n1 -lt $n2; echo $?
0

[root@localhost ~]# test $n1 -ne  $n2; echo $?
0

测试的标志代表意义
5. 判定字串的数据
test -z string判定字串是否为0,若 string 为空字串,则为 true
test ! -z string判定字串是否非为0,若 string 为空字串,则为 false
test str1 == str2判定 str1 是否等于 str2,若相等,则回传 true
test str1 != str2判定 str1 是否不等于 str2,若相等,则回传 false

注意:
这里的 string 可以是实际的字符串,也可以是一个变量
这里说的字符串是否为 0 的意思是 字符串的长度是否为 0

示例:

[root@localhost ~]# test -z ''; echo $?      # 空字符串
0

[root@localhost ~]# test -z ' '; echo $?      # 含有一个空格的字符串
1

[root@localhost ~]# test ! -z ' '; echo $?   # 判断含有一个空格的字符串,其长度为非0的字符串, 空格也算是字符串
0

[root@localhost ~]# test -z ${name}; echo $?   # 变量未定义,shell 中认为其长度为 0
0

[root@localhost ~]# name=chen

[root@localhost ~]# test -z ${name}; echo $?
1

[root@localhost ~]# age=''                        # 定义变量,并且赋值为空字符串

[root@localhost ~]# test -z ${age}; echo $?    # shell 中,被赋值为空字符串的变量长度也为 0
0

[root@localhost ~]# name=chen
[root@localhost ~]# age=18
[root@localhost ~]# test $name == $age;echo $?
0
[root@localhost ~]# test $name != $age;echo $?
1

注意:
再次强调一下, 在 shell 中,以下两种情况,变量的长度均视为 0

  • 变量未定义
  • 变量定义了,但赋值为空字符串,比如 a=’’ , b=""

测试的标志代表意义
6. 多重条件判定,例如:test -r filename -a -x filename
-a(and) 两状况同时成立!例如 test -r file -a -x file,则 file 同时具有 r 与 x 权限时,才回传true
-o(or) 两状况任何一个成立!例如 test -r file -o -xfile,则 file 具有 r 或 x 权限时,就可回传true
!反相状态,如test ! -x file, 当file不具有 x 时,回传true

示例:

[root@localhost ~]# ls -l a.txt
-rwxr-xr-x. 1 root root 0 2月  25 21:08 a.txt

[root@localhost ~]# test -r a.txt -a -x a.txt; echo $?
0

[root@localhost ~]# test -u a.txt -o -r a.txt; echo $?
0

-u 是否有SUID属性,这个条件为 fasle
-r 是否有可读属性,这个条件为 true
-o 两边任何一方返回true,最终整个判断表达式就返回true

#判断符号 []

[root@localhost ~]# [ -z "${HOME}" ] ; echo $?         # 判断$HOME是否为空字段,
0                                                      #是空字段
  • 必须要注意中括号的两端需要有空白字符来分隔
  • 在中括号 [] 内的每个元素之间都需要用空格来分隔;
  • 在中括号内的变量,最好都以双引号括号起来;
# 定义变量
[root@localhost ~]# name="chen"

# 开始测试值是否相等
[root@localhost ~]# [ "${name}" == "chao" ]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值