Execute Shell script after other script got executed successfully

14,264

Solution 1

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

Solution 2

You can simply run a chain of scripts in the command line (or from other script), when the first failing command will break this chain, using "&&" operator:

$ script1.sh && echo "First done, running the second" && script2.sh && echo "Second done, running the third" && script3.sh && echo "Third done, cool!"

And so on. The operation will break once one of the steps fails.

Share:
14,264
arsenal
Author by

arsenal

profile for ferhan on Stack Exchange, a network of free, community-driven Q&A sites http://stackexchange.com/users/flair/335839.png

Updated on June 12, 2022

Comments

  • arsenal
    arsenal almost 2 years

    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
    
  • arsenal
    arsenal over 11 years
    But in this case if some script got failed for any reason then I will not be able to know which one got failed right? Or there is some way to include those things here also?
  • arsenal
    arsenal over 11 years
    But in this case if some script got failed for any reason then I will not be able to know which one got failed right? Or there is some way to include those things here also?
  • kliteyn
    kliteyn over 11 years
    That's why there are all the "echo" commands there. You can see what is printed, so when the operation breaks, you know what failed according to what was printed. Anyway, it is just a simple example what "&&" can do - it does what your original script in the question does, but shorter. You can have much more complicated stuff written. Y
  • William Pursell
    William Pursell over 11 years
    In fact, you can get rid of the line continuations and write it on 4 lines. If the line ends in &&, the shell will continue the command on the next line without an explicit `\`.
  • Charles Duffy
    Charles Duffy over 11 years
    Should simply be if script1; then ... -- no need to store $? if it's only going to be used once to test if it's successful. Also, ending script names with .sh is bad practice; see talisman.org/~erlkonig/documents/…
  • Chris Dodd
    Chris Dodd over 11 years
    @WilliamPursell: I did not know that. Is that the case in all sh variants or just bash?
  • William Pursell
    William Pursell over 11 years
    @Chris It is standard. See the grammar for and_or at pubs.opengroup.org/onlinepubs/9699919799/utilities/…
  • Admin
    Admin almost 6 years
    @ChrisDodd I understand your answer. I am also working on same kind of issue in that I want to do if the first script fails don't run remaining and capture error log as well.