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

`if [-e file.txt]`不在bash中工作

发布时间:2020-12-15 22:50:12 所属栏目:安全 来源:网络整理
导读:我正在尝试使用bash检查文件是否存在.这是我的代码 if [-e file.txt]; then echo "file exists"else echo "file doesn't exist"fi 但当我运行它时,我得到: ./test.sh: line 3: [-e: command not found 我究竟做错了什么? 解决方法 [不是Bash中的特殊标记;
我正在尝试使用bash检查文件是否存在.这是我的代码

if [-e file.txt]; then
  echo "file exists"
else
  echo "file doesn't exist"
fi

但当我运行它时,我得到:

./test.sh: line 3: [-e: command not found

我究竟做错了什么?

解决方法

[不是Bash中的特殊标记;它只是单词[是一个内置命令(就像回声).所以你需要一个空间.而且,类似地,你需要一个空格]:

if [ -e file.txt ] ; then

也就是说,我推荐[[]] – 它在某些方面更安全(尽管它仍然需要空格):

if [[ -e file.txt ]] ; then

(编辑:李大同)

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

    推荐文章
      热点阅读