Executing multiple Bash commands after OR










0














I'm writing a Bash script to install some software; Is there a way to create a one liner that executes multiple commands after an OR? Basically, what I want to do for error checking is is this:



sudo apt-get install fortune || (echo "Installation failed" ; exit)
echo "Installation successful"


I've tried this, but it doesn't exit the script when the installation fails, and still outputs "Installation successful" as well. Any ideas on how to edit this method to make it work?










share|improve this question


























    0














    I'm writing a Bash script to install some software; Is there a way to create a one liner that executes multiple commands after an OR? Basically, what I want to do for error checking is is this:



    sudo apt-get install fortune || (echo "Installation failed" ; exit)
    echo "Installation successful"


    I've tried this, but it doesn't exit the script when the installation fails, and still outputs "Installation successful" as well. Any ideas on how to edit this method to make it work?










    share|improve this question
























      0












      0








      0







      I'm writing a Bash script to install some software; Is there a way to create a one liner that executes multiple commands after an OR? Basically, what I want to do for error checking is is this:



      sudo apt-get install fortune || (echo "Installation failed" ; exit)
      echo "Installation successful"


      I've tried this, but it doesn't exit the script when the installation fails, and still outputs "Installation successful" as well. Any ideas on how to edit this method to make it work?










      share|improve this question













      I'm writing a Bash script to install some software; Is there a way to create a one liner that executes multiple commands after an OR? Basically, what I want to do for error checking is is this:



      sudo apt-get install fortune || (echo "Installation failed" ; exit)
      echo "Installation successful"


      I've tried this, but it doesn't exit the script when the installation fails, and still outputs "Installation successful" as well. Any ideas on how to edit this method to make it work?







      linux bash error-handling scripting






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 9 at 21:21









      AndreasKralj

      1637




      1637






















          1 Answer
          1






          active

          oldest

          votes


















          4














          Parentheses create a subshell, which is what the exit commands exits. You want a command group, defined with braces.



          sudo apt-get install fortune || echo "Installation failed" >&2; exit 1; 


          (Note the last semicolon and the spaces around the braces; all are important.)



          To keep things more readable, typically you define a function to act as the single command following ||:



          abort () echo "Installation of $1 failed" >&2; exit 1; 

          sudo apt-get install fortune || abort fortune





          share|improve this answer






















          • Two additional suggestions: send the error message to stderr instead of stdout (that's what it's for), and exit with a nonzero status (to indicate there was an error).
            – Gordon Davisson
            Nov 9 at 21:38










          • This is perfect, thank you! If I wanted to get the exit code of the function that just executed, how would I send that to the abort function you just wrote? Would I be able to assign $? to a variable in it and then echo that?
            – AndreasKralj
            Nov 9 at 21:42







          • 2




            @AndreasKralj, consider die() retval=$?; echo "$*" >&2; exit $(( retval ? retval : 1 )); -- thus, foo || die "foo failed" will pass through the exit status of foo, whereas die whatever with a $? of 0 will use 1.
            – Charles Duffy
            Nov 9 at 21:47







          • 2




            This all become a lot easier if you stop trying to be excessively verbose. apt-get prints a perfectly reasonable error message, and you don't add much information by writing "Installation of $1 failed". If you simply omit that, you can just write sudo apt-get install fotrune || exit, and the value returned by sudo will be returned by the script. (exit with you argument is the same as exit $?, and if you want to be explicit just write exit $?).
            – William Pursell
            Nov 9 at 21:55










          • That's perfect @Charles Duffy, thank you very much!
            – AndreasKralj
            Nov 9 at 21:55











          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          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: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          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%2fstackoverflow.com%2fquestions%2f53233421%2fexecuting-multiple-bash-commands-after-or%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          4














          Parentheses create a subshell, which is what the exit commands exits. You want a command group, defined with braces.



          sudo apt-get install fortune || echo "Installation failed" >&2; exit 1; 


          (Note the last semicolon and the spaces around the braces; all are important.)



          To keep things more readable, typically you define a function to act as the single command following ||:



          abort () echo "Installation of $1 failed" >&2; exit 1; 

          sudo apt-get install fortune || abort fortune





          share|improve this answer






















          • Two additional suggestions: send the error message to stderr instead of stdout (that's what it's for), and exit with a nonzero status (to indicate there was an error).
            – Gordon Davisson
            Nov 9 at 21:38










          • This is perfect, thank you! If I wanted to get the exit code of the function that just executed, how would I send that to the abort function you just wrote? Would I be able to assign $? to a variable in it and then echo that?
            – AndreasKralj
            Nov 9 at 21:42







          • 2




            @AndreasKralj, consider die() retval=$?; echo "$*" >&2; exit $(( retval ? retval : 1 )); -- thus, foo || die "foo failed" will pass through the exit status of foo, whereas die whatever with a $? of 0 will use 1.
            – Charles Duffy
            Nov 9 at 21:47







          • 2




            This all become a lot easier if you stop trying to be excessively verbose. apt-get prints a perfectly reasonable error message, and you don't add much information by writing "Installation of $1 failed". If you simply omit that, you can just write sudo apt-get install fotrune || exit, and the value returned by sudo will be returned by the script. (exit with you argument is the same as exit $?, and if you want to be explicit just write exit $?).
            – William Pursell
            Nov 9 at 21:55










          • That's perfect @Charles Duffy, thank you very much!
            – AndreasKralj
            Nov 9 at 21:55
















          4














          Parentheses create a subshell, which is what the exit commands exits. You want a command group, defined with braces.



          sudo apt-get install fortune || echo "Installation failed" >&2; exit 1; 


          (Note the last semicolon and the spaces around the braces; all are important.)



          To keep things more readable, typically you define a function to act as the single command following ||:



          abort () echo "Installation of $1 failed" >&2; exit 1; 

          sudo apt-get install fortune || abort fortune





          share|improve this answer






















          • Two additional suggestions: send the error message to stderr instead of stdout (that's what it's for), and exit with a nonzero status (to indicate there was an error).
            – Gordon Davisson
            Nov 9 at 21:38










          • This is perfect, thank you! If I wanted to get the exit code of the function that just executed, how would I send that to the abort function you just wrote? Would I be able to assign $? to a variable in it and then echo that?
            – AndreasKralj
            Nov 9 at 21:42







          • 2




            @AndreasKralj, consider die() retval=$?; echo "$*" >&2; exit $(( retval ? retval : 1 )); -- thus, foo || die "foo failed" will pass through the exit status of foo, whereas die whatever with a $? of 0 will use 1.
            – Charles Duffy
            Nov 9 at 21:47







          • 2




            This all become a lot easier if you stop trying to be excessively verbose. apt-get prints a perfectly reasonable error message, and you don't add much information by writing "Installation of $1 failed". If you simply omit that, you can just write sudo apt-get install fotrune || exit, and the value returned by sudo will be returned by the script. (exit with you argument is the same as exit $?, and if you want to be explicit just write exit $?).
            – William Pursell
            Nov 9 at 21:55










          • That's perfect @Charles Duffy, thank you very much!
            – AndreasKralj
            Nov 9 at 21:55














          4












          4








          4






          Parentheses create a subshell, which is what the exit commands exits. You want a command group, defined with braces.



          sudo apt-get install fortune || echo "Installation failed" >&2; exit 1; 


          (Note the last semicolon and the spaces around the braces; all are important.)



          To keep things more readable, typically you define a function to act as the single command following ||:



          abort () echo "Installation of $1 failed" >&2; exit 1; 

          sudo apt-get install fortune || abort fortune





          share|improve this answer














          Parentheses create a subshell, which is what the exit commands exits. You want a command group, defined with braces.



          sudo apt-get install fortune || echo "Installation failed" >&2; exit 1; 


          (Note the last semicolon and the spaces around the braces; all are important.)



          To keep things more readable, typically you define a function to act as the single command following ||:



          abort () echo "Installation of $1 failed" >&2; exit 1; 

          sudo apt-get install fortune || abort fortune






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 9 at 21:41

























          answered Nov 9 at 21:34









          chepner

          244k31231323




          244k31231323











          • Two additional suggestions: send the error message to stderr instead of stdout (that's what it's for), and exit with a nonzero status (to indicate there was an error).
            – Gordon Davisson
            Nov 9 at 21:38










          • This is perfect, thank you! If I wanted to get the exit code of the function that just executed, how would I send that to the abort function you just wrote? Would I be able to assign $? to a variable in it and then echo that?
            – AndreasKralj
            Nov 9 at 21:42







          • 2




            @AndreasKralj, consider die() retval=$?; echo "$*" >&2; exit $(( retval ? retval : 1 )); -- thus, foo || die "foo failed" will pass through the exit status of foo, whereas die whatever with a $? of 0 will use 1.
            – Charles Duffy
            Nov 9 at 21:47







          • 2




            This all become a lot easier if you stop trying to be excessively verbose. apt-get prints a perfectly reasonable error message, and you don't add much information by writing "Installation of $1 failed". If you simply omit that, you can just write sudo apt-get install fotrune || exit, and the value returned by sudo will be returned by the script. (exit with you argument is the same as exit $?, and if you want to be explicit just write exit $?).
            – William Pursell
            Nov 9 at 21:55










          • That's perfect @Charles Duffy, thank you very much!
            – AndreasKralj
            Nov 9 at 21:55

















          • Two additional suggestions: send the error message to stderr instead of stdout (that's what it's for), and exit with a nonzero status (to indicate there was an error).
            – Gordon Davisson
            Nov 9 at 21:38










          • This is perfect, thank you! If I wanted to get the exit code of the function that just executed, how would I send that to the abort function you just wrote? Would I be able to assign $? to a variable in it and then echo that?
            – AndreasKralj
            Nov 9 at 21:42







          • 2




            @AndreasKralj, consider die() retval=$?; echo "$*" >&2; exit $(( retval ? retval : 1 )); -- thus, foo || die "foo failed" will pass through the exit status of foo, whereas die whatever with a $? of 0 will use 1.
            – Charles Duffy
            Nov 9 at 21:47







          • 2




            This all become a lot easier if you stop trying to be excessively verbose. apt-get prints a perfectly reasonable error message, and you don't add much information by writing "Installation of $1 failed". If you simply omit that, you can just write sudo apt-get install fotrune || exit, and the value returned by sudo will be returned by the script. (exit with you argument is the same as exit $?, and if you want to be explicit just write exit $?).
            – William Pursell
            Nov 9 at 21:55










          • That's perfect @Charles Duffy, thank you very much!
            – AndreasKralj
            Nov 9 at 21:55
















          Two additional suggestions: send the error message to stderr instead of stdout (that's what it's for), and exit with a nonzero status (to indicate there was an error).
          – Gordon Davisson
          Nov 9 at 21:38




          Two additional suggestions: send the error message to stderr instead of stdout (that's what it's for), and exit with a nonzero status (to indicate there was an error).
          – Gordon Davisson
          Nov 9 at 21:38












          This is perfect, thank you! If I wanted to get the exit code of the function that just executed, how would I send that to the abort function you just wrote? Would I be able to assign $? to a variable in it and then echo that?
          – AndreasKralj
          Nov 9 at 21:42





          This is perfect, thank you! If I wanted to get the exit code of the function that just executed, how would I send that to the abort function you just wrote? Would I be able to assign $? to a variable in it and then echo that?
          – AndreasKralj
          Nov 9 at 21:42





          2




          2




          @AndreasKralj, consider die() retval=$?; echo "$*" >&2; exit $(( retval ? retval : 1 )); -- thus, foo || die "foo failed" will pass through the exit status of foo, whereas die whatever with a $? of 0 will use 1.
          – Charles Duffy
          Nov 9 at 21:47





          @AndreasKralj, consider die() retval=$?; echo "$*" >&2; exit $(( retval ? retval : 1 )); -- thus, foo || die "foo failed" will pass through the exit status of foo, whereas die whatever with a $? of 0 will use 1.
          – Charles Duffy
          Nov 9 at 21:47





          2




          2




          This all become a lot easier if you stop trying to be excessively verbose. apt-get prints a perfectly reasonable error message, and you don't add much information by writing "Installation of $1 failed". If you simply omit that, you can just write sudo apt-get install fotrune || exit, and the value returned by sudo will be returned by the script. (exit with you argument is the same as exit $?, and if you want to be explicit just write exit $?).
          – William Pursell
          Nov 9 at 21:55




          This all become a lot easier if you stop trying to be excessively verbose. apt-get prints a perfectly reasonable error message, and you don't add much information by writing "Installation of $1 failed". If you simply omit that, you can just write sudo apt-get install fotrune || exit, and the value returned by sudo will be returned by the script. (exit with you argument is the same as exit $?, and if you want to be explicit just write exit $?).
          – William Pursell
          Nov 9 at 21:55












          That's perfect @Charles Duffy, thank you very much!
          – AndreasKralj
          Nov 9 at 21:55





          That's perfect @Charles Duffy, thank you very much!
          – AndreasKralj
          Nov 9 at 21:55


















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • 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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2fstackoverflow.com%2fquestions%2f53233421%2fexecuting-multiple-bash-commands-after-or%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

          𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

          Edmonton

          Crossroads (UK TV series)