Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute Shell script after other script got executed successfully

Problem Statement:-

I have four shell script that I want to execute only when the previous script got executed successfully. And I am running it like this currently-

./verify-export-realtime.sh

sh -x lca_query.sh

sh -x liv_query.sh

sh -x lqu_query.sh

So In order to make other scripts run after previous script was successful. I need to do something like below? I am not sure whether I am right? If any script got failed due to any reason it will print as Failed due to some reason right?

./verify-export-realtime.sh

RET_VAL_STATUS=$?
echo $RET_VAL_STATUS
if [ $RET_VAL_STATUS -ne 0 ]; then
echo "Failed due to some reason"
exit
fi

sh -x lca_query.sh

RET_VAL_STATUS=$?
echo $RET_VAL_STATUS
if [ $RET_VAL_STATUS -ne 0 ]; then
echo "Failed due to some reason"
exit
fi

sh -x liv_query.sh

RET_VAL_STATUS=$?
echo $RET_VAL_STATUS
if [ $RET_VAL_STATUS -ne 0 ]; then
echo "Failed due to some reason"
exit
fi


sh -x lqu_query.sh
like image 916
arsenal Avatar asked Jan 15 '23 07:01

arsenal


1 Answers

The shell provides an operator && to do exactly this. So you could write:

./verify-export-realtime.sh && \
sh -x lca_query.sh && \
sh -x liv_query.sh && \
sh -x lqu_query.sh

or you could get rid of the line continuations (\) and write it all on one line

./verify-export-realtime.sh && sh -x lca_query.sh && sh -x liv_query.sh && sh -x lqu_query.sh

If you want to know how far it got, you can add extra commands that just set a variable:

done=0
./verify-export-realtime.sh && done=1 &&
sh -x lca_query.sh && done=2 &&
sh -x liv_query.sh && done=3 &&
sh -x lqu_query.sh && done=4

The value of $done at the end tells you how many commands completed successfully. $? will get set to the exit value of the last command run (which is the one that failed), or 0 if all succeeded

like image 197
Chris Dodd Avatar answered Jan 26 '23 19:01

Chris Dodd