如何使用BashShell检查文件是否存在-创新互联
这篇文章主要介绍了如何使用Bash Shell检查文件是否存在,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

可以用如下的命令来进行检查:
test -e filename [ -e filename ] test -f filename [ -f filename ]
下面的命令,则使用Shell的条件表达式,判断/etc/hosts文件是否存在:
[ -f /etc/hosts ] && echo "Found" || echo "Not found"
该组合命令会输出以下内容:
Found
更常见的用法,则是将test命令放置在if..else..fi条件判断的条件表达式,然后在其中写上不同的分支逻辑
#!/bin/bash file="/etc/hosts" if [ -f "$file" ] then echo "$file found." else echo "$file not found." fi
检测文件属性的相关操作符
如果文件存在,并且具有相应的属性,如下的操作符都会返回true:
-b FILE FILE exists and is block special -c FILE FILE exists and is character special -d FILE FILE exists and is a directory -e FILE FILE exists -f FILE FILE exists and is a regular file -g FILE FILE exists and is set-group-ID -G FILE FILE exists and is owned by the effective group ID -h FILE FILE exists and is a symbolic link (same as -L) -k FILE FILE exists and has its sticky bit set -L FILE FILE exists and is a symbolic link (same as -h) -O FILE FILE exists and is owned by the effective user ID -p FILE FILE exists and is a named pipe -r FILE FILE exists and read permission is granted -s FILE FILE exists and has a size greater than zero -S FILE FILE exists and is a socket -t FD file descriptor FD is opened on a terminal -u FILE FILE exists and its set-user-ID bit is set -w FILE FILE exists and write permission is granted -x FILE FILE exists and execute (or search) permission is granted
以上命令,从man test复制而来。
使用上述符号的方法一模一样:
if [ operator FileName ] then echo "FileName - Found, take some action here" else echo "FileName - Not found, take some action here" fi
感谢你能够认真阅读完这篇文章,希望小编分享的“如何使用Bash Shell检查文件是否存在”这篇文章对大家有帮助,同时也希望大家多多支持创新互联网站建设公司,,关注创新互联行业资讯频道,更多相关知识等着你来学习!
名称栏目:如何使用BashShell检查文件是否存在-创新互联
本文来源:http://www.yantingwz.cn/article/diposd.html
其他资讯
- Eclipse中使用Ant打Android包报错及解决方案-创新互联
- 代运营靠谱吗?别被忽悠了,看看这些实例就知道了
- PostgreSQL源码解读(58)-查询语句#43(make_one_rel函数#8-B...
- c语言argv函数 c语言argv用法
- 面试guan:在 Java 中 new 一个对象的流程是怎样的?彻底被问懵了。。
- 代理服务器安全匿名 代理服务器信息
- 如何建立html文件
- 深入浅出理解Redis订阅发布功能(redis订阅和发布理解)
- 怎么分析oracle双网卡遇到的问题(怎么分析oracle双网卡遇到的问题)
- 手机怎么显示美国时间-手机时区设置,怎么样把手机时间设置成美国加州海沃德的时间


咨询
建站咨询
