shell脚本的条件判断2:文件属性的判断与比较
创始人
2024-04-13 05:54:32

一 文件属性的判断与比较

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

二 实例 

实例:文件和目录判断 

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

 判断文件是否存在:

root@ubuntu:/home/csdn# [ -e main.c ] && echo Y || echo N
Y
root@ubuntu:/home/csdn# [ -e nofile.c ] && echo Y || echo N
N
root@ubuntu:/home/csdn# [ ! -e nofile.c ] && echo Y || echo N
Y
root@ubuntu:/home/csdn# [ ! -e main.c ] && echo Y || echo N
N
root@ubuntu:/home/csdn#

目录判断:hello是一个目录


root@ubuntu:/home/csdn# [ -e hello ] && echo Y || echo N
Y
root@ubuntu:/home/csdn# [ -f hello ] && echo Y || echo N
N
root@ubuntu:/home/csdn# [ -d hello ] && echo Y || echo N
Y
root@ubuntu:/home/csdn#

实例:块设备测试 

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

root@host:~# [ -b /dev/mmcblk1p2 ] && echo Y || echo N
Y
root@host:~# [ -b /dev/mmcblk1p1 ] && echo Y || echo N
Y
root@host:~# [ -b /dev/mmcblk1p3 ] && echo Y || echo N
N
root@host:~#

实例:软/硬链接判断 

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

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

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

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

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

判断软链接: 

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

硬链接判断:

root@host:~# ln main.c hard.c
root@host:~# [ -f hard.c  ] && echo Y || echo N
Y
root@host:~# [ -L hard.c  ] && echo Y || echo N
N
root@host:~#

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

root@host:~# ls -l main.c
-rw-rw-rw-    2 root     root             0 Jan  1 05:58 main.c
root@host:~# chmod -rw main.c
root@host:~# ls -ls main.c1 ----------    2 root     root             6 Jan  1 06:06 main.c
root@host:~# echo "hello" > main.c
root@host:~# cat main.c
hello
root@host:~# ./main.c
-sh: ./main.c: Permission denied
root@host:~#

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

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

csdn@ubuntu:~$ ls -ls main.c
4 -rw-rw-r-- 1 csdn csdn 93 11月 26 11:04 main.c
csdn@ubuntu:~$ [ -r main.c ] && echo Y || echo N
Y
csdn@ubuntu:~$ [ -w main.c ] && echo Y || echo N
Y
csdn@ubuntu:~$ [ -x main.c ] && echo Y || echo N
N
csdn@ubuntu:~$ chmod -rw main.c
csdn@ubuntu:~$ [ -r main.c ] && echo Y || echo N
N
csdn@ubuntu:~$ [ -w main.c ] && echo Y || echo N
N
csdn@ubuntu:~$ [ -x main.c ] && echo Y || echo N
N
csdn@ubuntu:~$

实例:判断文件非空 

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

root@host:~# [ -s newfile ] && echo Y || echo N
N
root@host:~# echo "hehe" > newfile
root@host:~# ls -ls newfile1 -rw-rw-rw-    1 root     root             5 Jan  1 06:09 newfile
root@host:~# [ -s newfile ] && echo Y || echo N
Y
root@host:~#

实例:对比文件时间 

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

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

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

 文件main.c比other.c更旧

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

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

csdn@ubuntu:~$ [ main.c -ot main.c ] && echo Y || echo N
N
csdn@ubuntu:~$ [ main.c -nt main.c ] && echo Y || echo N
N
csdn@ubuntu:~$

 小结

相关内容

热门资讯

马斯克力捧,中国AI算力或领跑...   近期,据媒体报道,特斯拉CEO马斯克表示,中国在AI算力方面将会领先世界,关键优势在于其电力供应...
603606,拟花7300万元... 每经记者|赵李南    每经编辑|段炼 张益铭     1月9日,东方电缆(SH6...
特朗普开会大声朗读鲁比奥的小纸... 【#特朗普开会大声朗读鲁比奥的小纸条##特朗普朗读鲁比奥小纸条万斯大笑#】1月9日,美国总统特朗普与...
WTT新加坡大满贯首批参赛名单... 来源:央视新闻客户端 ​​WTT新加坡大满贯2026由新加坡圣淘沙名胜世界呈献,将于2026年2月1...
小米汽车: 新一代小米SU7在... 格隆汇1月10日|小米汽车发布答网友问表示,新一代小米SU7在各个方面都进行了大幅升级,因此其整备质...