首页 > 系统相关 >Shell文件属性的判断与比较

Shell文件属性的判断与比较

时间:2022-08-25 21:56:52浏览次数:151  
标签:file1 Shell 文件属性 echo centos7 判断 && txt root

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

[root@centos7~]# touch file1.txt
[root@centos7~]# touch file2.txt
[root@centos7~]# mkdir test
[root@centos7~]# [ -e file1.txt ] && echo对|| echo 错   #判断文件是否存在对
[root@centos7~]# [ -e test ] && echo对|| echo 错       #判断目录是否存在对
[root@centos7~]# [ ! -e file1.txt ] && echo对|| echo 错 #判断文件是否不存在错
[root@centos7~]# [ -f file1.txt ] && echo对|| echo 错   #判断存在,且为文件对
[root@centos7~]# [ ! -f file1.txt ] && echo对|| echo 错 #判断该文件不存在错
[root@centos7~]# [ -f test/ ] && echo对|| echo 错      #因为不是文件,结果错错

[root@centos7~]# [ -r file1.txt ] && echo Y || echo NY
[root@centos7~]# chmod -r file1.txt                        #删除r权限
[root@centos7~]# [ -r file1.txt ] && echo Y || echo N      #测试结果依然为真Y
[root@centos7~]# [ ! -r file1.txt ] && echo Y || echo N   #测试不可读N
[root@centos7~]# chmod -w file1.txt                        #删除w权限
[root@centos7~]# ls -l file1.txt----------. 1 root root 0 8月 16 20:31 file1.txt
[root@centos7~]# [ -w file1.txt ] && echo Y || echo N      #测试结果依然为真Y
[root@centos7~]# [ -x file1.txt ] && echo Y || echo N      #测试结果为假N
[root@centos7~]# chmod +x file1.txt                        #添加x权限
[root@centos7~]# ls -l file1.txt---x--x--x. 1 root root 0 8月 16 20:31 ver1.txt
[root@centos7~]# [ -x file1.txt ] && echo Y || echo NY

 

标签:file1,Shell,文件属性,echo,centos7,判断,&&,txt,root
From: https://www.cnblogs.com/xiong97/p/16625866.html

相关文章