检查 Linux bash 中文件是否存在

4

我正在尝试检查文件是否存在,如果存在,脚本将执行某些操作。但问题是它无法识别文件是否存在。

if [ -e /temp/file.txt ]; then
        echo "file found!"
        sudo cp -r temp/* extra
else
        echo "file not found! Creating new one..."
        ./create.sh
fi

以下是我正在测试的目录中文件的示例。 它们显然存在,但出于某种原因,我无法让脚本看到它们。 我做错了什么?
nima@mkt:/docs/text$ ls -a temp
.  ..  more  file.txt  file2.txt
3个回答

12

您在测试中使用了绝对路径,但应该使用相对路径:

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

看起来我拼写目录的时候有些错误。不管怎样,谢谢你的帮助 :P - Painguy

2

/temp/file.txt vs /docs/text/temp/file.txt?


1

你的脚本在/temp中查找,而你正在查找/docs/text/temp


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接