dimanche 26 novembre 2017

Bash - break conditional clause if one of the statements return error code != 0

I have the following bash script which runs on my CI and intends to run my code on a physical MacOS and on several docker Linux images:

if [[ "$OS_NAME" == "mac_os" ]]; then
       make all;
       run_test1;
       run_test2;
       make install;
    else
       docker exec -i my_docker_image bash -c "make all";
       docker exec -i my_docker_image bash -c "run_test1";
       docker exec -i my_docker_image bash -c "run_test2"; 
       docker exec -i my_docker_image bash -c "make install";
    fi

If the tests fail (run_test1 or run_test2) they return error code 1. If they pass they return error code 0.

The whole script runs with set -e so whenever it sees exit code other than 0 it stops and fails the entire build.

The problem is that currently, when run_test1 and run_test2 are inside the conditional clause - when they fail and return error code 1 the conditional clause doesn't break and the build succeeds although tests didn't pass.

So I have 2 questions:

  • How to break the conditional clause if one of the commands return error code other than 0?
  • How to break the conditional clause in such a way that the entire clause will return an error code (so the whole build will fail)?

Aucun commentaire:

Enregistrer un commentaire