检查文件是否存在并继续,否则在 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
有任何指示吗?
I have a script that is one script in a chain of others that sends an email.
At the start of the script I want to check if a file exists and continue only if it exists, otherwise just quit.
Here is the start of my script:
if [ ! -f /scripts/alert ];
then
echo "File not found!" && exit 0
else
continue
fi
However I keep getting a message saying:
line 10: continue: only meaningful in a `for', `while', or `until' loop
Any pointers?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将其更改为:
条件不是循环,并且没有您需要跳转到的地方。无论如何,执行只是在条件之后继续。
(我还删除了不必要的
&&
。并不是说它应该发生,但万一echo
失败,没有理由不退出。)Change it to this:
A conditional isn't a loop, and there's no place you need to jump to. Execution simply continues after the conditional anyway.
(I also removed the needless
&&
. Not that it should happen, but just in case theecho
fails there's no reason not to exit.)您的问题在于
continue
行,该行通常用于跳到for
或while
循环的下一个迭代。因此,只需删除脚本的
else
部分就可以让它工作。Your problem is with the
continue
line which is normally used to skip to the next iteration of afor
orwhile
loop.Therefore just removing the
else
part of your script should allow it to work.是的。删除
否则继续
。这是完全没有必要的。Yes. Drop the
else continue
. It's entirely unneeded.