How to make `trap` know if the EXIT is after successful program finish or because of premature as an error or something 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 Results Why I closed the “Why is Kali so hard” questionError 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

What is this single-engine low-wing propeller plane?

Gastric acid as a weapon

"Seemed to had" is it correct?

Did Xerox really develop the first LAN?

IndentationError when pasting code in Python 3 interpreter mode

Should I use Javascript Classes or Apex Classes in Lightning Web Components?

Withdrew £2800, but only £2000 shows as withdrawn on online banking; what are my obligations?

Single word antonym of "flightless"

Sorting numerically

Can a non-EU citizen traveling with me come with me through the EU passport line?

How can I make names more distinctive without making them longer?

How widely used is the term Treppenwitz? Is it something that most Germans know?

What makes black pepper strong or mild?

Antler Helmet: Can it work?

Why don't the Weasley twins use magic outside of school if the Trace can only find the location of spells cast?

Why is black pepper both grey and black?

Do I really need recursive chmod to restrict access to a folder?

Right-skewed distribution with mean equals to mode?

Is there a "higher Segal conjecture"?

Output the ŋarâþ crîþ alphabet song without using (m)any letters

Does surprise arrest existing movement?

Is it true that "carbohydrates are of no use for the basal metabolic need"?

Does accepting a pardon have any bearing on trying that person for the same crime in a sovereign jurisdiction?

Bonus calculation: Am I making a mountain out of a molehill?



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



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 Results
Why I closed the “Why is Kali so hard” questionError 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;








4















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()
echo "Failed to remove the install directory!!!!!!!!"; exit 155;

###############################################################################################################################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






























    4















    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()
    echo "Failed to remove the install directory!!!!!!!!"; exit 155;

    ###############################################################################################################################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


























      4












      4








      4


      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()
      echo "Failed to remove the install directory!!!!!!!!"; exit 155;

      ###############################################################################################################################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()
      echo "Failed to remove the install directory!!!!!!!!"; exit 155;

      ###############################################################################################################################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 yesterday









      muru

      37.9k590166




      37.9k590166










      asked 2 days ago









      somethingSomethingsomethingSomething

      1,860103460




      1,860103460




















          1 Answer
          1






          active

          oldest

          votes


















          7














          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









            7














            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



























              7














              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

























                7












                7








                7







                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 2 days ago









                GillesGilles

                548k13011131631




                548k13011131631



























                    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

                    Get product attribute by attribute group code in magento 2get product attribute by product attribute group in magento 2Magento 2 Log Bundle Product Data in List Page?How to get all product attribute of a attribute group of Default attribute set?Magento 2.1 Create a filter in the product grid by new attributeMagento 2 : Get Product Attribute values By GroupMagento 2 How to get all existing values for one attributeMagento 2 get custom attribute of a single product inside a pluginMagento 2.3 How to get all the Multi Source Inventory (MSI) locations collection in custom module?Magento2: how to develop rest API to get new productsGet product attribute by attribute group code ( [attribute_group_code] ) in magento 2

                    Category:9 (number) SubcategoriesMedia in category "9 (number)"Navigation menuUpload mediaGND ID: 4485639-8Library of Congress authority ID: sh85091979ReasonatorScholiaStatistics

                    Magento 2.3: How do i solve this, Not registered handle, on custom form?How can i rewrite TierPrice Block in Magento2magento 2 captcha not rendering if I override layout xmlmain.CRITICAL: Plugin class doesn't existMagento 2 : Problem while adding custom button order view page?Magento 2.2.5: Overriding Admin Controller sales/orderMagento 2.2.5: Add, Update and Delete existing products Custom OptionsMagento 2.3 : File Upload issue in UI Component FormMagento2 Not registered handleHow to configured Form Builder Js in my custom magento 2.3.0 module?Magento 2.3. How to create image upload field in an admin form