26

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?

jww
  • 97,681
  • 90
  • 411
  • 885
user1190083
  • 405
  • 1
  • 4
  • 5
  • 2
    Why do you need the 'continue' at all here? – Niall Byrne Feb 05 '12 at 01:25
  • Possible duplicate of [How do I tell if a regular file does not exist in Bash?](https://stackoverflow.com/questions/638975/how-do-i-tell-if-a-regular-file-does-not-exist-in-bash) – jww Mar 31 '18 at 22:05

3 Answers3

44

Change it to this:

{
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.

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

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
2

Your problem is with the continue line which is normally used to skip to the next iteration of a for or while loop.

Therefore just removing the else part of your script should allow it to work.

Christopher Bottoms
  • 11,218
  • 8
  • 50
  • 99
Pluckerpluck
  • 731
  • 6
  • 21
1

Yes. Drop the else continue. It's entirely unneeded.

Kevin
  • 53,822
  • 15
  • 101
  • 132