How to force abnormal termination of a bash script

3

I've a Synology NAS running DSM. I've created a bash script to check if a certain file is present within a certain folder. The script runs by a schedule and returns the result to me by email.

The problem is I get too many emails just saying the script ran ok.

The DSM task scheduler allows to send the email only if script terminates abnormally.

My question is: how can I force the script to terminate abnormally?

I would do so in order to get an email if the file I'm looking for doesn't exist.

naio

Posted 2016-10-21T12:12:43.667

Reputation: 33

Answers

4

You can use exit 1 to terminate the script.

Exit code 0 means everything went fine, all other indicate some kind of error.

Aki

Posted 2016-10-21T12:12:43.667

Reputation: 399

2

You could use an exit code if the file is missing:

#!/bin/bash
file="/foo/bar"
if [ -e "$file" ]
then
exit 0
else
exit 1
fi

[ -e ] is an operator which checks if "$file" exists. It is equivalent to test -e $file but suited to if-else-fi.
See man test if you need more operators.

Edit:
To further elaborate, there are also other exit codes which you may use for convenience, assuming you have a different email warning for when the script 'blows up' or does not execute for some reason.

fragamemnon

Posted 2016-10-21T12:12:43.667

Reputation: 316