22

我有一个脚本,它是一连串发送电子邮件的脚本中的一个。

在脚本的开头,我想检查一个文件是否存在,只有在它存在时才继续,否则就退出。

这是我的脚本的开始:

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

任何指针?

4

3 回答 3

41

将其更改为:

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

条件不是循环,没有地方需要跳转。无论如何,执行只是在条件之后继续。

(我还删除了不必要的&&。并不是说它应该发生,但以防万一echo失败,没有理由不退出。)

于 2012-02-05T01:25:47.020 回答
2

您的问题在于continue通常用于跳到fororwhile循环的下一次迭代的行。

因此,只需删除else脚本的一部分就可以让它工作。

于 2012-02-05T01:26:17.913 回答
1

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

于 2012-02-05T01:26:00.973 回答