Insert this line at the top of your script:
set -o errexit
This will cause bash to terminate the script on very first command failure.
Question
I auto-generate a file using a wrapper that performs a series of commands in parallel like this
C1 p1 p2 p3 > logfile1 2>& 1 & # call is I_1
C1 p1 p2 p3 > logfile2 2>& 1 & # I_2
C1 p1 p2 p3 > logfile3 2>& 1 &
C1 p1 p2 p3 > logfile4 2>& 1 &
The file is automatically generated already by the wrapper process What I'd like to do is this. If I_1 fails then terminate everything I_2 onwards.
I guess one way of doing this is finding the 1st instance of 2>& 1 &
and removing the '&' at the end to 2>& 1
which will run it in fg and then add these lines { rc= $? && [ "$rc" -ne 0 ] && return $rc ; }
to the following line . So following this - the outcome would look like this
C1 p1 p2 p3 > logfile1 2>& 1 # removed &
{ rc=$? && [ "$rc" -ne 0 ] && return $rc ; }
C1 p1 p2 p3 > logfile2 2>& 1 & # I_2
C1 p1 p2 p3 > logfile3 2>& 1 &
C1 p1 p2 p3 > logfile4 2>& 1 &
Some AWK or Sed would do the job . Or is there is a better approach to implement this.
Solution
Insert this line at the top of your script:
set -o errexit
This will cause bash to terminate the script on very first command failure.