1

I would like my script ends working if a specific file is not found. Therefore I write the following:

[ -f "$DAEMON" ] || (echo "File $DAEMON not found" && exit 0)

But it doesn't work properly. If the file doesn't exist, my script outputs this error message and continues working.

I tried

[ -f "$DAEMON" ] || { echo "File $DAEMON not found" && exit 0 }

(as it is suggested here exit doesn't work in bash ), but I got syntax error (unexpected end of file).

[ -f "$DAEMON" ] || exit 0 works well but [ -f "$DAEMON" ] || (exit 0) doesn't.

How can I output error message and stop script executing?

0

6 Answers 6

8

Your compound command (the { ... } construct) requires the semi-colon before the closing brace - it isn't optional! So this will work:

[ -f "$DAEMON" ] || { echo "File $DAEMON not found" && exit 0; }
Sign up to request clarification or add additional context in comments.

1 Comment

semicolon or the closing brace could be in the new line.
4

I think you'll probably find that, in someErrorProgram || (exit 0), the exit 0 is being run in a sub-shell due to the parentheses. That means the exit will exit from the sub-shell rather than the shell you think it's running in.

But, unless you're absolutely desperate to keep it on one line, I'd just use:

if [[ ! -f "$DAEMON" ]] ; then
    echo "File $DAEMON not found"
    exit 0
fi

2 Comments

Nicer solution than mine is :D
Yes, I know about if statement, but for me it is interesting how to do it on one line.
4

In such scenarios I using my errecho function, like

errecho() { echo "$@" >&2; return 1; }

the return 1 allows me chain the error condition, e.g. like

[[ -f file ]] || errecho "file doesnt exists" || exit 1

and it is useful for deeper functions too like

action() {

     [[ some cond ]] || errecho "errmsg" || return 1
}

do_something() {
    action "$1" "$3" || errecho "action failed with $3" || return 1
}

#main
do_something || errecho "total fail " || exit 1

Comments

3

use if statement:

if [ ! -f "$DAEMON" ]
then
  echo "File $DAEMON not found"
  exit 0
fi

2 Comments

Yes, I know about if statement, but for me it is interesting how to do it on one line.
Your solution lacks not operator (!) before -f
1

You can use this if you want a single line solution and it works

[[ -f "filename" ]] || { echo "file doesnt exists" && exit ;}

echo "bye"

I didn't got the bye in console if the file not.

1 Comment

@jm666 : Oh yes. My idea was to stop the script but anyway I just modified the script to make it work even it found the file.I found the semicolon is missing and did that.
0

Use a loop and if statement. With the echo you can check until which point the script went. In the current case the script breaks if it encounters Daemon or C and so only echos A and B.

for i in A B Daemon C; do

if 
[ $i == Daemon ] || 
[ $i == C ]

then
    break
fi

echo "$i"

done

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.