`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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |