Abort the execution if one script have errors

Gents,

I have a script which will run others scripts .

Example

#!/bin/bash

script1
script2
script3
script4

I would like to stop all process if any of the script got errors?. If for example script1 works fine will continue script2, then if the script2 got errors, The bash should stop and will no continue to script3.

Is there any solution for this.

Thanks for your help.

How will you know if script2 errors?

If it issues a return code, this will be in the variable $? until you do something else. You can test for that:-

:
:
scripts2
if [ $? -ne 0 ]
then
   any abort action or message
   exit
fi
script3
:
:

You can make your script exit with a non-zero return code by putting in exit 5 or whatever integer value you want (within reason)

Another way to do this is to make them dependant on each other with the && operator. This will move to the next if the return code is zero but quit that statement (carries on the the overall script) if any one of them fails:-

script1 && script2 && script3 && script4

echo "This always runs"

Do either of these meet your needs?

Regards,
Robin

1 Like

Did you consider bash 's -e option?

1 Like