Shell文件属性的判断与比较
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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律