【Linux】test命令

检测系统是否包含某些文件或者相关属性时,test命令是个很好的命令, 加上 && 及 || 还能更人性化地显示结果

# test -e new && echo "exist" || echo "not exist"

not exist

更多用法:

测试的标志代表意义
1. 关於某个档名的『文件类型』判断,如 test -e filename 表示存在否
-e该『档名』是否存在?(常用)
-f该『档名』是否存在且为文件(file)?(常用)
-d该『档名』是否存在且为目录(directory)?(常用)
-b该『档名』是否存在且为一个 block device 装置?
-c该『档名』是否存在且为一个 character device 装置?
-S该『档名』是否存在且为一个 Socket 文件?
-p该『档名』是否存在且为一个 FIFO (pipe) 文件?
-L该『档名』是否存在且为一个连结档?
2. 关於文件的权限侦测,如 test -r filename 表示可读否 (但 root 权限常有例外)
-r侦测该档名是否存在且具有『可读』的权限?
-w侦测该档名是否存在且具有『可写』的权限?
-x侦测该档名是否存在且具有『可运行』的权限?
-u侦测该档名是否存在且具有『SUID』的属性?
-g侦测该档名是否存在且具有『SGID』的属性?
-k侦测该档名是否存在且具有『Sticky bit』的属性?
-s侦测该档名是否存在且为『非空白文件』?
3. 两个文件之间的比较,如: test file1 -nt file2
-nt(newer than)判断 file1 是否比 file2 新
-ot(older than)判断 file1 是否比 file2 旧
-ef判断 file1 与 file2 是否为同一文件,可用在判断 hard link 的判定上。 主要意义在判定,两个文件是否均指向同一个 inode 哩!
4. 关於两个整数之间的判定,例如 test n1 -eq n2
-eq两数值相等 (equal)
-ne两数值不等 (not equal)
-gtn1 大於 n2 (greater than)
-ltn1 小於 n2 (less than)
-gen1 大於等於 n2 (greater than or equal)
-len1 小於等於 n2 (less than or equal)
5. 判定字串的数据
test -z string判定字串是否为 0 ?若 string 为空字串,则为 true
test -n string判定字串是否非为 0 ?若 string 为空字串,则为 false。
注: -n 亦可省略
test str1 = str2判定 str1 是否等於 str2 ,若相等,则回传 true
test str1 != str2判定 str1 是否不等於 str2 ,若相等,则回传 false
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 -x file,则 file 具有 r 或 x 权限时,就可回传 true。
!反相状态,如 test ! -x file ,当 file 不具有 x 时,回传 true


利用 test 来帮我们写几个简单的例子。首先,判断一下,让使用者输入一个档名,我们判断:

  1. 这个文件是否存在,若不存在则给予一个『Filename does not exist』的信息,并中断程序;
  2. 若这个文件存在,则判断他是个文件或目录,结果输出『Filename is regular file』或 『Filename is directory』
  3. 判断一下,运行者的身份对这个文件或目录所拥有的权限,并输出权限数据

    #!/bin/bash
    echo -e "pls input a filename, I will check the filename;s type and \
    permission.\n\n"
    read -p "input a filename: " filename
    test ! -e $filename && echo "the fils is not existed!" && exit 0
    test -f $filename && filetype="filename is regular file."
    test -d $filename && filetype="filename is directory."
    test -r $filename && perm="readable"
    test -w $filename && perm="writable"
    test -x $filename && perm="executable"
    
    echo "the filename: $filename is a $filetype"
    echo "and the permissions are: $perm"
    测试
    # ./sh05.sh 
    pls input a filename, I will check the filename;s type and permission.




    input a filename: mi
    the fils is not existed!
    [root@~ /root]
    # ./sh05.sh 
    pls input a filename, I will check the filename;s type and permission.




    input a filename: log
    the filename: log is a filename is regular file.
    and the permissions are: writable

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值