gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 09:02:02 26 4
gpt4 key购买 nike

我正在尝试使用 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 中的特殊标记;只是 word [ 是一个内置命令(就像 echo)。所以你需要在它后面有一个空格。同样,您需要在 ] 之前有一个空格:

if [ -e file.txt ] ; then

也就是说,我建议改为使用 [[ ]] — 它在某些方面更安全(尽管它仍然需要空格):

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

关于bash - `if [-e file.txt]` 在 bash 中不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9635208/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com