加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

linux – 如何检查文件是否存在

发布时间:2020-12-14 02:38:28 所属栏目:Linux 来源:网络整理
导读:如何使用 shell脚本确定文件是否存在? 即: #!/bin/shif [ Does File Exist? ]then do this thingfi 解决方法 你可能想要/ bin / bash,除非你需要使用/ bin / sh,/ bin / sh更受限制.所以如果你使用bash: 像这样: if [[ -e filename ]]; then echo 'exist
如何使用 shell脚本确定文件是否存在?

即:

#!/bin/sh

if [ Does File Exist? ]
then
    do this thing
fi

解决方法

你可能想要/ bin / bash,除非你需要使用/ bin / sh,/ bin / sh更受限制.所以如果你使用bash:

像这样:

if [[ -e filename ]]; then
    echo 'exists'
 fi

如果您的文件名在变量中,那么使用以下内容,如果文件中有空格,则双引号很重要:

if [[ -e "$myFile" ]]; then
   echo 'exists'
fi

如果您使用sh,并希望与IEEE Std 1003.1,2004版兼容,则使用单个括号代替.仍然支持-e开关.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读