检查文件是否存在,然后继续在Bash中退出 [英] Check if file exists and continue else exit in Bash

查看:1190
本文介绍了检查文件是否存在,然后继续在Bash中退出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

这是我的脚本的开始:

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
}

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

A conditional isn't a loop, and there's no place you need to jump to. Execution simply continues after the conditional anyway.

(我还删除了不必要的&& 。不是这样的应该发生,但是如果 echo 失败,没有理由不退出。)

(I also removed the needless &&. Not that it should happen, but just in case the echo fails there's no reason not to exit.)

这篇关于检查文件是否存在,然后继续在Bash中退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆