0

I am running club.sh

inside club.sh script i am running below scripts.

test1.sh test2.sh test3.sh

my concern is it should run one by one and if test1 fails it will not run test2.sh and if test2.sh fails it willnot run test3.sh

how can we check? can any one suggest any idea it would be very helpful.

Thanks,

  • You can add `set -e` as the line after the shebang to exit the script immediately if anything fails. Try running `help set`. – Mark Setchell May 03 '17 at 14:16
  • @MarkSetchell I think that `set -e` might be a bit too harsh for this scenario (from the info we have). If the only condition for early termination is the failure of the `test*` scripts then their exit codes should be examined as opposed to killing `club.sh` on *any* error. – Lix May 03 '17 at 14:20

4 Answers4

4

Two approaches -

First, you can examine the exit code of each of your inner scripts (test1.sh, test2.sh, ...) and decide whether to continue accordingly -

$? Will return the exit code of the previous command. It will be 0 (zero) if the script exited without an error. Anything that is not 0 can be considered a failure. So you could so something like this -

./test1.sh # execute script
if [[ $? != 0 ]]; then exit; fi # check return value, exit if not 0

Alternatively, you could use the && bash operator which will only execute subsequent commands if the previous one passed -

./test1.sh && ./test2.sh && test3.sh 

Only if test1.sh returns an exit code of 0 (zero) will test2.sh execute and the same goes for test3.sh.


The first approach is good if you need to do some logging or cleanup between executing your scripts, but if you are only concerned that the execution should not continue if there was a failure then the && method would be they way I recommend.


Here is a related post dealing with the meaning behind &&

Community
  • 1
  • 1
Lix
  • 47,311
  • 12
  • 103
  • 131
  • 1
    The exit code can be used directly without test `./test2.sh || exit 2`. In my opinon easier to read. And nobody is tempted to put in an additional line with an `echo "somthing important"` – ULick May 03 '17 at 17:33
1

The returned value of the execution of the first command/script is stored in $? so using this value you can check if your command was successfully executed.

Try this:

bash test1.sh
if [ $? -eq 0 ]; then # if script succeeded
    bash test2.sh
else
    echo "script failed"
fi
Amani Ben Azzouz
  • 2,477
  • 3
  • 15
  • 26
1

If you want to exit your script whenever a command fails, you just add at the beginning of your script set -e.

#!/bin/bash

set -e

echo hello
ls /root/lalala
echo world

Otherwise, you have two options.

The first one is to use &&. For instance:

echo hello && ls /some_inexistant_directory && echo world

The second one is to check the return value after each command:

#!/bin/bash

echo toto
if [ "$?" != "0" ]; then
    exit 1
fi

ls /root
if [ "$?" != "0" ]; then
    exit 1
fi

echo world
if [ "$?" != "0" ]; then
    exit 1
fi
yoones
  • 2,394
  • 1
  • 16
  • 20
  • Please take a look at [my comment here](http://stackoverflow.com/questions/43762381/how-to-exit-from-running-if-any-one-of-script-fails#comment74566630_43762381). Using `set -e` will terminate `club.sh` on *any* error - not only for errors within the `test*` scripts. – Lix May 03 '17 at 14:21
  • @Lix I know, I'm just listing options here. – yoones May 03 '17 at 14:25
0

You just have to put the below at the begging of the script:

#!/bin/bash -e 
J. Chomel
  • 8,193
  • 15
  • 41
  • 69