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

use*_*083 18 bash if-statement continue

我有一个脚本,它是发送电子邮件的其他链中的一个脚本.

在脚本开始时,我想检查文件是否存在,只有在存在时才继续,否则只是退出.

这是我的脚本的开始:

if [ ! -f /scripts/alert ];
then
    echo "File not found!" && exit 0
else
        continue
fi
Run Code Online (Sandbox Code Playgroud)

但是我不断收到一条消息说:

line 10: continue: only meaningful in a `for', `while', or `until' loop
Run Code Online (Sandbox Code Playgroud)

有什么指针吗?

Ker*_* SB 34

把它改成这个:

{
if [ ! -f /scripts/alert ]; then
    echo "File not found!"
    exit 0
fi
}
Run Code Online (Sandbox Code Playgroud)

条件不是循环,并且您无需跳转到任何地方.无论如何,执行只是在条件之后继续.

(我也删除了不必要的东西&&.不是它应该发生,但万一echo失败,没有理由不退出.)