检查文件是否存在,并继续在Bash中退出

我有一个脚本是发送电子邮件的其他人链中的一个脚本。

在脚本开始时,我想检查一个文件是否存在,如果存在则继续,否则就退出。

这是我的脚本的开始:

if [ ! -f /scripts/alert ];
then
    echo "File not found!" && exit 0
else
        continue
fi

不过,我不断收到一条消息:

line 10: continue: only meaningful in a `for', `while', or `until' loop

任何指针?


将其更改为:

{
if [ ! -f /scripts/alert ]; then
    echo "File not found!"
    exit 0
fi
}

一个条件不是一个循环,也没有你需要跳转到的地方。 无论如何,有条件地继续执行。

(我也删除了不必要的&& ,并不是说它会发生,但是为了防止echo失败,没有理由不退出。)


是。 放下else continue 。 这完全不需要。


你的问题是通常用于跳过forwhile循环的下一个循环的continue行。

因此,只要删除脚本的else部分就可以使其工作。

链接地址: http://www.djcxy.com/p/24137.html

上一篇: Check if file exists and continue else exit in Bash

下一篇: How can I check a file exists and execute a command if not?