How to make `trap` know if the EXIT is after successful program finish or because of premature as an error or something The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) 2019 Community Moderator Election ResultsError handling in shell scriptHow to trigger error using Trap commandPrevent SIGINT from interrupting function call and child process(es) withinHow to keep last exit status after testKeep exit codes when trapping SIGINT and similar?How to make `local` capture the exit code?Restoring tty correctly with sttyPerl: Error code returned to open3 after killed programexceptions to ERR trapShell_exec of dynamically created script with else

How did passengers keep warm on sail ships?

In horse breeding, what is the female equivalent of putting a horse out "to stud"?

Format single node in tikzcd

"... to apply for a visa" or "... and applied for a visa"?

How to stretch delimiters to envolve matrices inside of a kbordermatrix?

Wall plug outlet change

Simulating Exploding Dice

Mortgage adviser recommends a longer term than necessary combined with overpayments

What can I do if neighbor is blocking my solar panels intentionally?

How did the audience guess the pentatonic scale in Bobby McFerrin's presentation?

Why does tar appear to skip file contents when output file is /dev/null?

Finding the path in a graph from A to B then back to A with a minimum of shared edges

What do you call a plan that's an alternative plan in case your initial plan fails?

How does ice melt when immersed in water?

How to colour the US map with Yellow, Green, Red and Blue to minimize the number of states with the colour of Green

How long does the line of fire that you can create as an action using the Investiture of Flame spell last?

How to make `trap` know if the EXIT is after successful program finish or because of premature as an error or something

What aspect of planet Earth must be changed to prevent the industrial revolution?

University's motivation for having tenure-track positions

Can the DM override racial traits?

Would an alien lifeform be able to achieve space travel if lacking in vision?

Is there a trick to getting spices to fix to nuts?

Who or what is the being for whom Being is a question for Heidegger?

How do you keep chess fun when your opponent constantly beats you?



How to make `trap` know if the EXIT is after successful program finish or because of premature as an error or something



The 2019 Stack Overflow Developer Survey Results Are In
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
2019 Community Moderator Election ResultsError handling in shell scriptHow to trigger error using Trap commandPrevent SIGINT from interrupting function call and child process(es) withinHow to keep last exit status after testKeep exit codes when trapping SIGINT and similar?How to make `local` capture the exit code?Restoring tty correctly with sttyPerl: Error code returned to open3 after killed programexceptions to ERR trapShell_exec of dynamically created script with else



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








2















PROBLEM:



I have a shell program that I have been writing but I can't find out how to make sure that trap is trapping for cleanup at the end or because of a error in some command, it cleans up either way.



Here is the code:



################################### Successful exit then this cleanup ###########################################################3

successfulExit()

###############################################################################################################################33
####### Catch the program on successful exit and cleanup
trap successfulExit EXIT


QUESTION:



How can I make trap only trap EXIT on program finish?



Here is the full script:



debianConfigAwsome.5.3.sh










share|improve this question






























    2















    PROBLEM:



    I have a shell program that I have been writing but I can't find out how to make sure that trap is trapping for cleanup at the end or because of a error in some command, it cleans up either way.



    Here is the code:



    ################################### Successful exit then this cleanup ###########################################################3

    successfulExit()

    ###############################################################################################################################33
    ####### Catch the program on successful exit and cleanup
    trap successfulExit EXIT


    QUESTION:



    How can I make trap only trap EXIT on program finish?



    Here is the full script:



    debianConfigAwsome.5.3.sh










    share|improve this question


























      2












      2








      2


      1






      PROBLEM:



      I have a shell program that I have been writing but I can't find out how to make sure that trap is trapping for cleanup at the end or because of a error in some command, it cleans up either way.



      Here is the code:



      ################################### Successful exit then this cleanup ###########################################################3

      successfulExit()

      ###############################################################################################################################33
      ####### Catch the program on successful exit and cleanup
      trap successfulExit EXIT


      QUESTION:



      How can I make trap only trap EXIT on program finish?



      Here is the full script:



      debianConfigAwsome.5.3.sh










      share|improve this question
















      PROBLEM:



      I have a shell program that I have been writing but I can't find out how to make sure that trap is trapping for cleanup at the end or because of a error in some command, it cleans up either way.



      Here is the code:



      ################################### Successful exit then this cleanup ###########################################################3

      successfulExit()

      ###############################################################################################################################33
      ####### Catch the program on successful exit and cleanup
      trap successfulExit EXIT


      QUESTION:



      How can I make trap only trap EXIT on program finish?



      Here is the full script:



      debianConfigAwsome.5.3.sh







      shell-script exit-status trap






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 2 hours ago









      muru

      37.6k589165




      37.6k589165










      asked 7 hours ago









      somethingSomethingsomethingSomething

      1,850103460




      1,850103460




















          1 Answer
          1






          active

          oldest

          votes


















          4














          On entry to the EXIT trap, $? contains the exit status. That's the same value you'd find as $? after calling this script in another shell: either the argument passed to exit (truncated to the range 0–255) or the return status of the preceding command. In the case of an exit due to set -e, it's the return status of the command that triggered the implicit exit.



          Usually you should save $? and exit again with the same status.



          cleanup () 
          if [ -n "$1" ]; then
          echo "Aborted by $1"
          elif [ $status -ne 0 ]; then
          echo "Failure (status $status)"
          else
          echo "Success"
          fi

          trap 'status=$?; cleanup; exit $status' EXIT
          trap 'trap - HUP; cleanup SIGHUP; kill -HUP $$' HUP
          trap 'trap - INT; cleanup SIGINT; kill -INT $$' INT
          trap 'trap - TERM; cleanup SIGTERM; kill -TERM $$' TERM





          share|improve this answer























            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%2f512331%2fhow-to-make-trap-know-if-the-exit-is-after-successful-program-finish-or-becaus%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














            On entry to the EXIT trap, $? contains the exit status. That's the same value you'd find as $? after calling this script in another shell: either the argument passed to exit (truncated to the range 0–255) or the return status of the preceding command. In the case of an exit due to set -e, it's the return status of the command that triggered the implicit exit.



            Usually you should save $? and exit again with the same status.



            cleanup () 
            if [ -n "$1" ]; then
            echo "Aborted by $1"
            elif [ $status -ne 0 ]; then
            echo "Failure (status $status)"
            else
            echo "Success"
            fi

            trap 'status=$?; cleanup; exit $status' EXIT
            trap 'trap - HUP; cleanup SIGHUP; kill -HUP $$' HUP
            trap 'trap - INT; cleanup SIGINT; kill -INT $$' INT
            trap 'trap - TERM; cleanup SIGTERM; kill -TERM $$' TERM





            share|improve this answer



























              4














              On entry to the EXIT trap, $? contains the exit status. That's the same value you'd find as $? after calling this script in another shell: either the argument passed to exit (truncated to the range 0–255) or the return status of the preceding command. In the case of an exit due to set -e, it's the return status of the command that triggered the implicit exit.



              Usually you should save $? and exit again with the same status.



              cleanup () 
              if [ -n "$1" ]; then
              echo "Aborted by $1"
              elif [ $status -ne 0 ]; then
              echo "Failure (status $status)"
              else
              echo "Success"
              fi

              trap 'status=$?; cleanup; exit $status' EXIT
              trap 'trap - HUP; cleanup SIGHUP; kill -HUP $$' HUP
              trap 'trap - INT; cleanup SIGINT; kill -INT $$' INT
              trap 'trap - TERM; cleanup SIGTERM; kill -TERM $$' TERM





              share|improve this answer

























                4












                4








                4







                On entry to the EXIT trap, $? contains the exit status. That's the same value you'd find as $? after calling this script in another shell: either the argument passed to exit (truncated to the range 0–255) or the return status of the preceding command. In the case of an exit due to set -e, it's the return status of the command that triggered the implicit exit.



                Usually you should save $? and exit again with the same status.



                cleanup () 
                if [ -n "$1" ]; then
                echo "Aborted by $1"
                elif [ $status -ne 0 ]; then
                echo "Failure (status $status)"
                else
                echo "Success"
                fi

                trap 'status=$?; cleanup; exit $status' EXIT
                trap 'trap - HUP; cleanup SIGHUP; kill -HUP $$' HUP
                trap 'trap - INT; cleanup SIGINT; kill -INT $$' INT
                trap 'trap - TERM; cleanup SIGTERM; kill -TERM $$' TERM





                share|improve this answer













                On entry to the EXIT trap, $? contains the exit status. That's the same value you'd find as $? after calling this script in another shell: either the argument passed to exit (truncated to the range 0–255) or the return status of the preceding command. In the case of an exit due to set -e, it's the return status of the command that triggered the implicit exit.



                Usually you should save $? and exit again with the same status.



                cleanup () 
                if [ -n "$1" ]; then
                echo "Aborted by $1"
                elif [ $status -ne 0 ]; then
                echo "Failure (status $status)"
                else
                echo "Success"
                fi

                trap 'status=$?; cleanup; exit $status' EXIT
                trap 'trap - HUP; cleanup SIGHUP; kill -HUP $$' HUP
                trap 'trap - INT; cleanup SIGINT; kill -INT $$' INT
                trap 'trap - TERM; cleanup SIGTERM; kill -TERM $$' TERM






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 6 hours ago









                GillesGilles

                547k13011131631




                547k13011131631



























                    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%2f512331%2fhow-to-make-trap-know-if-the-exit-is-after-successful-program-finish-or-becaus%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

                    Magento 2 - Add success message with knockout Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?Success / Error message on ajax request$.widget is not a function when loading a homepage after add custom jQuery on custom themeHow can bind jQuery to current document in Magento 2 When template load by ajaxRedirect page using plugin in Magento 2Magento 2 - Update quantity and totals of cart page without page reload?Magento 2: Quote data not loaded on knockout checkoutMagento 2 : I need to change add to cart success message after adding product into cart through pluginMagento 2.2.5 How to add additional products to cart from new checkout step?Magento 2 Add error/success message with knockoutCan't validate Post Code on checkout page

                    Fil:Tokke komm.svg

                    Where did Arya get these scars? Unicorn Meta Zoo #1: Why another podcast? Announcing the arrival of Valued Associate #679: Cesar Manara Favourite questions and answers from the 1st quarter of 2019Why did Arya refuse to end it?Has the pronunciation of Arya Stark's name changed?Has Arya forgiven people?Why did Arya Stark lose her vision?Why can Arya still use the faces?Has the Narrow Sea become narrower?Does Arya Stark know how to make poisons outside of the House of Black and White?Why did Nymeria leave Arya?Why did Arya not kill the Lannister soldiers she encountered in the Riverlands?What is the current canonical age of Sansa, Bran and Arya Stark?