• shell脚本的条件判断2:文件属性的判断与比较


    一 文件属性的判断与比较

    Shell支持大量对文件属性的判断,常用的文件属性操作符很多,如下表所示。更多文件属性操作符可以参考命令帮助手册(man test)。

    二 实例 

    实例:文件和目录判断 

     可以创建新的文件,也可以使用已有的文件进行测试。

     判断文件是否存在:

    1. root@ubuntu:/home/csdn# [ -e main.c ] && echo Y || echo N
    2. Y
    3. root@ubuntu:/home/csdn# [ -e nofile.c ] && echo Y || echo N
    4. N
    5. root@ubuntu:/home/csdn# [ ! -e nofile.c ] && echo Y || echo N
    6. Y
    7. root@ubuntu:/home/csdn# [ ! -e main.c ] && echo Y || echo N
    8. N
    9. root@ubuntu:/home/csdn#

    目录判断:hello是一个目录

    1. root@ubuntu:/home/csdn# [ -e hello ] && echo Y || echo N
    2. Y
    3. root@ubuntu:/home/csdn# [ -f hello ] && echo Y || echo N
    4. N
    5. root@ubuntu:/home/csdn# [ -d hello ] && echo Y || echo N
    6. Y
    7. root@ubuntu:/home/csdn#

    实例:块设备测试 

    下面这个测试,假设系统中有某个磁盘设备,使用-b测试该设备是否存在,且当该设备为块设备时返回值为Y,否则返回值为N。

    1. root@host:~# [ -b /dev/mmcblk1p2 ] && echo Y || echo N
    2. Y
    3. root@host:~# [ -b /dev/mmcblk1p1 ] && echo Y || echo N
    4. Y
    5. root@host:~# [ -b /dev/mmcblk1p3 ] && echo Y || echo N
    6. N
    7. root@host:~#

    实例:软/硬链接判断 

     Linux系统中的文件链接分为软链接和硬链接两种。软链接创建后,如果源文件被删除,则软链接将无法继续使用,可以跨分区和磁盘创建软链接。硬链接创建后,如果源文件被删除,则硬链接依然可以正常使用、正常读写数据,但硬链接不可以跨分区或磁盘创建。另外,硬链接与源文件使用的是相同的设备、相同的inode编号。使用ls -l(小写字母L)命令查看硬链接文件的属性时,文件属性与普通文件是一样的,而软链接的文件属性则可以看到被l(小写字母L)标记,表示该文件为软链接。

    虽然文件main.c不存在,但是依然可以创建一个指向它的软链接 

    1. root@host:~# ln -s main.c softlink.c
    2. root@host:~# ls -l main.c softlink.c
    3. ls: main.c: No such file or directory
    4. lrwxrwxrwx 1 root root 6 Jan 1 05:55 softlink.c -> main.c
    5. root@host:~#

     此时指向的main.c是红色的

     使用touch创建了一个main.c,链接颜色正常了

    判断软链接: 

    1. root@host:~# [ -L softlink.c ] && echo Y || echo N
    2. Y
    3. root@host:~#

    硬链接判断:

    1. root@host:~# ln main.c hard.c
    2. root@host:~# [ -f hard.c ] && echo Y || echo N
    3. Y
    4. root@host:~# [ -L hard.c ] && echo Y || echo N
    5. N
    6. root@host:~#

    在测试权限时需要注意,超级管理员root在没有rw权限的情况下,也是可以读写文件的,rw权限对超级管理员是无效的。但是如果文件没有x权限,哪怕是root也不可以执行该文件。

    1. root@host:~# ls -l main.c
    2. -rw-rw-rw- 2 root root 0 Jan 1 05:58 main.c
    3. root@host:~# chmod -rw main.c
    4. root@host:~# ls -ls main.c
    5. 1 ---------- 2 root root 6 Jan 1 06:06 main.c
    6. root@host:~# echo "hello" > main.c
    7. root@host:~# cat main.c
    8. hello
    9. root@host:~# ./main.c
    10. -sh: ./main.c: Permission denied
    11. root@host:~#

    为了获得相对可参考的值,切换用户为普通用户:

     测试之前,文件main.c可读可写,通过chmod -rw后,文件不可读,不可写。

    1. csdn@ubuntu:~$ ls -ls main.c
    2. 4 -rw-rw-r-- 1 csdn csdn 93 11月 26 11:04 main.c
    3. csdn@ubuntu:~$ [ -r main.c ] && echo Y || echo N
    4. Y
    5. csdn@ubuntu:~$ [ -w main.c ] && echo Y || echo N
    6. Y
    7. csdn@ubuntu:~$ [ -x main.c ] && echo Y || echo N
    8. N
    9. csdn@ubuntu:~$ chmod -rw main.c
    10. csdn@ubuntu:~$ [ -r main.c ] && echo Y || echo N
    11. N
    12. csdn@ubuntu:~$ [ -w main.c ] && echo Y || echo N
    13. N
    14. csdn@ubuntu:~$ [ -x main.c ] && echo Y || echo N
    15. N
    16. csdn@ubuntu:~$

    实例:判断文件非空 

     默认touch命令创建的文件都是空文件,在使用-s测试文件是否为非空文件时,因为文件是空文件,所以测试结果为假。当文件中有内容时,测试文件是否为非空时,结果为真。

    1. root@host:~# [ -s newfile ] && echo Y || echo N
    2. N
    3. root@host:~# echo "hehe" > newfile
    4. root@host:~# ls -ls newfile
    5. 1 -rw-rw-rw- 1 root root 5 Jan 1 06:09 newfile
    6. root@host:~# [ -s newfile ] && echo Y || echo N
    7. Y
    8. root@host:~#

    实例:对比文件时间 

    现在可以使用测试条件判断两个文件的创建时间,看看哪个文件是新文件,哪个文件是旧文件。new than表示更新,old than表示更旧。根据下面的输出结果可知,main.c文件比other.c文件更旧。 

    首先,看下两个文件的信息: 

    1. csdn@ubuntu:~$ ls main.c other.c -ls
    2. 4 ---------- 1 csdn csdn 93 11月 26 11:04 main.c
    3. 4 -rw-rw-r-- 1 csdn csdn 89 11月 26 11:10 other.c
    4. csdn@ubuntu:~$

     文件main.c比other.c更旧

    1. csdn@ubuntu:~$ [ main.c -nt other.c ] && echo Y || echo N
    2. N
    3. csdn@ubuntu:~$ [ main.c -ot other.c ] && echo Y || echo N
    4. Y
    5. csdn@ubuntu:~$

    文件main.c自己和自己比呢?既不新也不旧。

    1. csdn@ubuntu:~$ [ main.c -ot main.c ] && echo Y || echo N
    2. N
    3. csdn@ubuntu:~$ [ main.c -nt main.c ] && echo Y || echo N
    4. N
    5. csdn@ubuntu:~$

     小结

  • 相关阅读:
    江湖再见:毫米波雷达开发手册之行为识别应用
    通过Pyecharts绘制可视化地球竟 然如此简单
    有了 C 语言的基础,怎么学 Java ?
    喜报|百华鞋业成功入选2022 年临沂市内外贸产品“三同”企业名单
    对于BP算法全矩阵传播及偏置项的一些理解
    一个音频水印的小例子
    30天Python入门(第九天:深入了解Python中的条件语句)
    Internet Download Manager2022中文版免费下载
    实验室LIMS系统 asp.net源码 lims系统源码
    python读取.txt文件中某些关键字后面的内容 并根据该数据画图
  • 原文地址:https://blog.csdn.net/yueni_zhao/article/details/128052412