Bash - run one script when previous is sucessful else run another script?Bash - how to run a command after...

Why don't electrons take the shorter path in coils?

What to say to a student who has failed?

Can pay be witheld for hours cleaning up after closing time?

How do I request a longer than normal leave of absence period for my wedding?

Does travel insurance for short flight delays exist?

Did the British navy fail to take into account the ballistics correction due to Coriolis force during WW1 Falkland Islands battle?

Sun setting in East!

Why can't an Airbus A330 dump fuel in an emergency?

See details of old sessions

How to use "Du hast/ Du hattest'?

Is a player able to change alignment midway through an adventure?

Why is Boris Johnson visiting only Paris & Berlin if every member of the EU needs to agree on a withdrawal deal?

Why does The Ancient One think differently about Doctor Strange in Endgame than the film Doctor Strange?

What is the history of the university asylum law?

Why were movies shot on film shot at 24 frames per second?

Does norwegian.no airline overbook flights?

How should I face my manager if I make a mistake because a senior coworker explained something incorrectly to me?

Are there account age or level requirements for obtaining special research?

In an emergency, how do I find and share my position?

Singleton Design Pattern implementation in a not traditional way

Is there any practical application for performing a double Fourier transform? ...or an inverse Fourier transform on a time-domain input?

Start from ones

What is the hex versus octal timeline?

Why is my Earth simulation slower than the reality?



Bash - run one script when previous is sucessful else run another script?


Bash - how to run a command after the previous finished?Run a script after some command were executedCron automated bash script to run run 1 bash script then another, plus integrity checkDouble click Linux Bash Script and debug with pause commandsBash script to extract message id and force sendBash trap in function, but executed when entire script exits?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







1















I think my question is similar to this one but with one extrat step - say I have 3 scripts:




  1. main_script.sh

  2. report_success.sh

  3. report_failure.sh


How can I do something like this pseudo-code does:



if (main_script.sh finished without error):
run report_success.sh
else:
run report_failure.sh


?



Thanks!










share|improve this question



























  • I guess you are aware tht the syntax you show is not bash. It looks like python.

    – guillermo chamorro
    2 days ago













  • Thanks for your comment. I just edited my question.

    – user3768495
    2 days ago


















1















I think my question is similar to this one but with one extrat step - say I have 3 scripts:




  1. main_script.sh

  2. report_success.sh

  3. report_failure.sh


How can I do something like this pseudo-code does:



if (main_script.sh finished without error):
run report_success.sh
else:
run report_failure.sh


?



Thanks!










share|improve this question



























  • I guess you are aware tht the syntax you show is not bash. It looks like python.

    – guillermo chamorro
    2 days ago













  • Thanks for your comment. I just edited my question.

    – user3768495
    2 days ago














1












1








1








I think my question is similar to this one but with one extrat step - say I have 3 scripts:




  1. main_script.sh

  2. report_success.sh

  3. report_failure.sh


How can I do something like this pseudo-code does:



if (main_script.sh finished without error):
run report_success.sh
else:
run report_failure.sh


?



Thanks!










share|improve this question
















I think my question is similar to this one but with one extrat step - say I have 3 scripts:




  1. main_script.sh

  2. report_success.sh

  3. report_failure.sh


How can I do something like this pseudo-code does:



if (main_script.sh finished without error):
run report_success.sh
else:
run report_failure.sh


?



Thanks!







bash shell-script control-flow






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 2 days ago







user3768495

















asked 2 days ago









user3768495user3768495

1646 bronze badges




1646 bronze badges
















  • I guess you are aware tht the syntax you show is not bash. It looks like python.

    – guillermo chamorro
    2 days ago













  • Thanks for your comment. I just edited my question.

    – user3768495
    2 days ago



















  • I guess you are aware tht the syntax you show is not bash. It looks like python.

    – guillermo chamorro
    2 days ago













  • Thanks for your comment. I just edited my question.

    – user3768495
    2 days ago

















I guess you are aware tht the syntax you show is not bash. It looks like python.

– guillermo chamorro
2 days ago







I guess you are aware tht the syntax you show is not bash. It looks like python.

– guillermo chamorro
2 days ago















Thanks for your comment. I just edited my question.

– user3768495
2 days ago





Thanks for your comment. I just edited my question.

– user3768495
2 days ago










3 Answers
3






active

oldest

votes


















2















Assuming your main_script.sh is fairly standard and passes a 0 for a successful run and 1 for an unsuccessful run, you can probe the status via $? like so:



./main_script.sh
if [ $? -eq 0 ]; then
./report_success.sh
else
./report_failure.sh
fi





share|improve this answer


























  • There is almost never a reason to compare or even use $? directly, unless you need to save it for later (which you don't need to here).

    – Kusalananda
    2 days ago



















4















if main_script.sh; then
report_success.sh
else
report_failure.sh
fi


This uses the exit status of main_script.sh to determine whether it was successful (exit status 0) or not (exit status non-zero) and then runs the appropriate report script.






share|improve this answer










New contributor



dhanlin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





























    1















    It's as simple as this:



    true && echo ok || echo fail



    false && echo ok || echo fail



    A typical command may return null (as true does) or may return not null (as false does) depending on whether the operation succeeded or failed.



    ./main_script.sh && ./report_success.sh || ./report_failure.sh



    If you had to run several commands for the ok or fail case, use braces. No need for several scripts:



    ./main_script.sh && { echo ok ; echo ok2 } || { echo fail ; echo fail2 ; }






    share|improve this answer


























    • Note that your commands with && and || would run ./report_failure.sh if either of ./main_script.sh or ./report_success.sh failed. This is not the same thing as an explicit if-then-else.

      – Kusalananda
      2 days ago













    • Well, yes. But failure of the success report is a failure, too.

      – Janka
      2 days ago














    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "106"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f536539%2fbash-run-one-script-when-previous-is-sucessful-else-run-another-script%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2















    Assuming your main_script.sh is fairly standard and passes a 0 for a successful run and 1 for an unsuccessful run, you can probe the status via $? like so:



    ./main_script.sh
    if [ $? -eq 0 ]; then
    ./report_success.sh
    else
    ./report_failure.sh
    fi





    share|improve this answer


























    • There is almost never a reason to compare or even use $? directly, unless you need to save it for later (which you don't need to here).

      – Kusalananda
      2 days ago
















    2















    Assuming your main_script.sh is fairly standard and passes a 0 for a successful run and 1 for an unsuccessful run, you can probe the status via $? like so:



    ./main_script.sh
    if [ $? -eq 0 ]; then
    ./report_success.sh
    else
    ./report_failure.sh
    fi





    share|improve this answer


























    • There is almost never a reason to compare or even use $? directly, unless you need to save it for later (which you don't need to here).

      – Kusalananda
      2 days ago














    2














    2










    2









    Assuming your main_script.sh is fairly standard and passes a 0 for a successful run and 1 for an unsuccessful run, you can probe the status via $? like so:



    ./main_script.sh
    if [ $? -eq 0 ]; then
    ./report_success.sh
    else
    ./report_failure.sh
    fi





    share|improve this answer













    Assuming your main_script.sh is fairly standard and passes a 0 for a successful run and 1 for an unsuccessful run, you can probe the status via $? like so:



    ./main_script.sh
    if [ $? -eq 0 ]; then
    ./report_success.sh
    else
    ./report_failure.sh
    fi






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered 2 days ago









    Jason K LaiJason K Lai

    2797 bronze badges




    2797 bronze badges
















    • There is almost never a reason to compare or even use $? directly, unless you need to save it for later (which you don't need to here).

      – Kusalananda
      2 days ago



















    • There is almost never a reason to compare or even use $? directly, unless you need to save it for later (which you don't need to here).

      – Kusalananda
      2 days ago

















    There is almost never a reason to compare or even use $? directly, unless you need to save it for later (which you don't need to here).

    – Kusalananda
    2 days ago





    There is almost never a reason to compare or even use $? directly, unless you need to save it for later (which you don't need to here).

    – Kusalananda
    2 days ago













    4















    if main_script.sh; then
    report_success.sh
    else
    report_failure.sh
    fi


    This uses the exit status of main_script.sh to determine whether it was successful (exit status 0) or not (exit status non-zero) and then runs the appropriate report script.






    share|improve this answer










    New contributor



    dhanlin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.


























      4















      if main_script.sh; then
      report_success.sh
      else
      report_failure.sh
      fi


      This uses the exit status of main_script.sh to determine whether it was successful (exit status 0) or not (exit status non-zero) and then runs the appropriate report script.






      share|improve this answer










      New contributor



      dhanlin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.
























        4














        4










        4









        if main_script.sh; then
        report_success.sh
        else
        report_failure.sh
        fi


        This uses the exit status of main_script.sh to determine whether it was successful (exit status 0) or not (exit status non-zero) and then runs the appropriate report script.






        share|improve this answer










        New contributor



        dhanlin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        if main_script.sh; then
        report_success.sh
        else
        report_failure.sh
        fi


        This uses the exit status of main_script.sh to determine whether it was successful (exit status 0) or not (exit status non-zero) and then runs the appropriate report script.







        share|improve this answer










        New contributor



        dhanlin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.








        share|improve this answer



        share|improve this answer








        edited 2 days ago









        Kusalananda

        160k18 gold badges318 silver badges504 bronze badges




        160k18 gold badges318 silver badges504 bronze badges






        New contributor



        dhanlin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.








        answered 2 days ago









        dhanlindhanlin

        414 bronze badges




        414 bronze badges




        New contributor



        dhanlin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.




        New contributor




        dhanlin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.




























            1















            It's as simple as this:



            true && echo ok || echo fail



            false && echo ok || echo fail



            A typical command may return null (as true does) or may return not null (as false does) depending on whether the operation succeeded or failed.



            ./main_script.sh && ./report_success.sh || ./report_failure.sh



            If you had to run several commands for the ok or fail case, use braces. No need for several scripts:



            ./main_script.sh && { echo ok ; echo ok2 } || { echo fail ; echo fail2 ; }






            share|improve this answer


























            • Note that your commands with && and || would run ./report_failure.sh if either of ./main_script.sh or ./report_success.sh failed. This is not the same thing as an explicit if-then-else.

              – Kusalananda
              2 days ago













            • Well, yes. But failure of the success report is a failure, too.

              – Janka
              2 days ago
















            1















            It's as simple as this:



            true && echo ok || echo fail



            false && echo ok || echo fail



            A typical command may return null (as true does) or may return not null (as false does) depending on whether the operation succeeded or failed.



            ./main_script.sh && ./report_success.sh || ./report_failure.sh



            If you had to run several commands for the ok or fail case, use braces. No need for several scripts:



            ./main_script.sh && { echo ok ; echo ok2 } || { echo fail ; echo fail2 ; }






            share|improve this answer


























            • Note that your commands with && and || would run ./report_failure.sh if either of ./main_script.sh or ./report_success.sh failed. This is not the same thing as an explicit if-then-else.

              – Kusalananda
              2 days ago













            • Well, yes. But failure of the success report is a failure, too.

              – Janka
              2 days ago














            1














            1










            1









            It's as simple as this:



            true && echo ok || echo fail



            false && echo ok || echo fail



            A typical command may return null (as true does) or may return not null (as false does) depending on whether the operation succeeded or failed.



            ./main_script.sh && ./report_success.sh || ./report_failure.sh



            If you had to run several commands for the ok or fail case, use braces. No need for several scripts:



            ./main_script.sh && { echo ok ; echo ok2 } || { echo fail ; echo fail2 ; }






            share|improve this answer













            It's as simple as this:



            true && echo ok || echo fail



            false && echo ok || echo fail



            A typical command may return null (as true does) or may return not null (as false does) depending on whether the operation succeeded or failed.



            ./main_script.sh && ./report_success.sh || ./report_failure.sh



            If you had to run several commands for the ok or fail case, use braces. No need for several scripts:



            ./main_script.sh && { echo ok ; echo ok2 } || { echo fail ; echo fail2 ; }







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered 2 days ago









            JankaJanka

            3065 bronze badges




            3065 bronze badges
















            • Note that your commands with && and || would run ./report_failure.sh if either of ./main_script.sh or ./report_success.sh failed. This is not the same thing as an explicit if-then-else.

              – Kusalananda
              2 days ago













            • Well, yes. But failure of the success report is a failure, too.

              – Janka
              2 days ago



















            • Note that your commands with && and || would run ./report_failure.sh if either of ./main_script.sh or ./report_success.sh failed. This is not the same thing as an explicit if-then-else.

              – Kusalananda
              2 days ago













            • Well, yes. But failure of the success report is a failure, too.

              – Janka
              2 days ago

















            Note that your commands with && and || would run ./report_failure.sh if either of ./main_script.sh or ./report_success.sh failed. This is not the same thing as an explicit if-then-else.

            – Kusalananda
            2 days ago







            Note that your commands with && and || would run ./report_failure.sh if either of ./main_script.sh or ./report_success.sh failed. This is not the same thing as an explicit if-then-else.

            – Kusalananda
            2 days ago















            Well, yes. But failure of the success report is a failure, too.

            – Janka
            2 days ago





            Well, yes. But failure of the success report is a failure, too.

            – Janka
            2 days ago


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Unix & Linux Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f536539%2fbash-run-one-script-when-previous-is-sucessful-else-run-another-script%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Taj Mahal Inhaltsverzeichnis Aufbau | Geschichte | 350-Jahr-Feier | Heutige Bedeutung | Siehe auch |...

            Baia Sprie Cuprins Etimologie | Istorie | Demografie | Politică și administrație | Arii naturale...

            Nicolae Petrescu-Găină Cuprins Biografie | Opera | In memoriam | Varia | Controverse, incertitudini...