Execute code in a logout script if exiting from a particular directory?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
3
down vote

favorite












I have accounts on a number of different machines for testing a couple libraries I contribute to. Some of the machines are owned by others (like the GCC compile farm), and I'd like to keep them tidy.



I want to setup a .bash_logout script that performs a make clean upon exit if I am in a particular directory. The directory requirement drops out of multiple SSH sessions. If I am building/testing in one session, I don't want a separate session logout cleaning the artifacts. That is, exit from $HOME/libfoo performs a clean; while exit from $HOME does not.



I understand the basics, but its not clear to me how robust this is:



# $HOME/.bash_logout

if [ "$PWD" = "$HOME/libfoo" ]; then
make clean 1>/dev/null 2>&1
fi


Are there any problems with using $PWD during logout? Are there any other problems that I might have missed?










share|improve this question

























    up vote
    3
    down vote

    favorite












    I have accounts on a number of different machines for testing a couple libraries I contribute to. Some of the machines are owned by others (like the GCC compile farm), and I'd like to keep them tidy.



    I want to setup a .bash_logout script that performs a make clean upon exit if I am in a particular directory. The directory requirement drops out of multiple SSH sessions. If I am building/testing in one session, I don't want a separate session logout cleaning the artifacts. That is, exit from $HOME/libfoo performs a clean; while exit from $HOME does not.



    I understand the basics, but its not clear to me how robust this is:



    # $HOME/.bash_logout

    if [ "$PWD" = "$HOME/libfoo" ]; then
    make clean 1>/dev/null 2>&1
    fi


    Are there any problems with using $PWD during logout? Are there any other problems that I might have missed?










    share|improve this question























      up vote
      3
      down vote

      favorite









      up vote
      3
      down vote

      favorite











      I have accounts on a number of different machines for testing a couple libraries I contribute to. Some of the machines are owned by others (like the GCC compile farm), and I'd like to keep them tidy.



      I want to setup a .bash_logout script that performs a make clean upon exit if I am in a particular directory. The directory requirement drops out of multiple SSH sessions. If I am building/testing in one session, I don't want a separate session logout cleaning the artifacts. That is, exit from $HOME/libfoo performs a clean; while exit from $HOME does not.



      I understand the basics, but its not clear to me how robust this is:



      # $HOME/.bash_logout

      if [ "$PWD" = "$HOME/libfoo" ]; then
      make clean 1>/dev/null 2>&1
      fi


      Are there any problems with using $PWD during logout? Are there any other problems that I might have missed?










      share|improve this question













      I have accounts on a number of different machines for testing a couple libraries I contribute to. Some of the machines are owned by others (like the GCC compile farm), and I'd like to keep them tidy.



      I want to setup a .bash_logout script that performs a make clean upon exit if I am in a particular directory. The directory requirement drops out of multiple SSH sessions. If I am building/testing in one session, I don't want a separate session logout cleaning the artifacts. That is, exit from $HOME/libfoo performs a clean; while exit from $HOME does not.



      I understand the basics, but its not clear to me how robust this is:



      # $HOME/.bash_logout

      if [ "$PWD" = "$HOME/libfoo" ]; then
      make clean 1>/dev/null 2>&1
      fi


      Are there any problems with using $PWD during logout? Are there any other problems that I might have missed?







      bash shell-script logout






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Feb 18 '16 at 7:14









      jww

      1,51132158




      1,51132158




















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          5
          down vote



          accepted










          Is your intent to execute the script only on log-out?

          From man bash:




          When a login shell exits, bash reads and executes commands from the file ~/.bash_logout, if it exists.




          Only on exit of a bash login shell.




          If you intend to run an script on every shell close, use trap (see man bash):



          trap /u1/myuser/on_exit_script.sh EXIT


          Add it to .bashrc or some other that works for you.




          Also, as the script will execute on exit, the $PWD will be the one active on exit, which may or may not be the same as when the shell was started. If you need to do something on the $PWD that was in use on exit, then yes, this test:



          if [ "$PWD" = "$HOME/libfoo" ]; then


          Should work.






          share|improve this answer



























            up vote
            0
            down vote













            I like many of my shell scripts portable and shell-agnostic, so I prefer trapping EXIT over using the bash-specific .bash_logout method. And for a truly self-contained solution it's best not having to modify or create .bash_logout, nor rely on a separate script to be run on exit. So for minimal impact on foreign systems, my preferred method is to drop a custom login profile in place whenever I start working on a system, and having that same profile clean up itself and any helper scripts or config files it creates. I create a shell function "bye" in the script that performs said cleanup, and register it for auto-cleanup on logout, whether intentional or by simple disconnection, via:



            trap bye EXIT




            share








            New contributor




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
























              up vote
              -1
              down vote













              I'm not sure how to do that through a bash_logout, though you could write a little script called exit that you'd put in /usr/local/bin/ with the kind of code that you put in your question, so that when you type exit in your console it'd do what you want if you are in the specific directory






              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',
                convertImagesToLinks: false,
                noModals: false,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: null,
                bindNavPrevention: true,
                postfix: "",
                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%2f264019%2fexecute-code-in-a-logout-script-if-exiting-from-a-particular-directory%23new-answer', 'question_page');

                );

                Post as a guest






























                3 Answers
                3






                active

                oldest

                votes








                3 Answers
                3






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes








                up vote
                5
                down vote



                accepted










                Is your intent to execute the script only on log-out?

                From man bash:




                When a login shell exits, bash reads and executes commands from the file ~/.bash_logout, if it exists.




                Only on exit of a bash login shell.




                If you intend to run an script on every shell close, use trap (see man bash):



                trap /u1/myuser/on_exit_script.sh EXIT


                Add it to .bashrc or some other that works for you.




                Also, as the script will execute on exit, the $PWD will be the one active on exit, which may or may not be the same as when the shell was started. If you need to do something on the $PWD that was in use on exit, then yes, this test:



                if [ "$PWD" = "$HOME/libfoo" ]; then


                Should work.






                share|improve this answer
























                  up vote
                  5
                  down vote



                  accepted










                  Is your intent to execute the script only on log-out?

                  From man bash:




                  When a login shell exits, bash reads and executes commands from the file ~/.bash_logout, if it exists.




                  Only on exit of a bash login shell.




                  If you intend to run an script on every shell close, use trap (see man bash):



                  trap /u1/myuser/on_exit_script.sh EXIT


                  Add it to .bashrc or some other that works for you.




                  Also, as the script will execute on exit, the $PWD will be the one active on exit, which may or may not be the same as when the shell was started. If you need to do something on the $PWD that was in use on exit, then yes, this test:



                  if [ "$PWD" = "$HOME/libfoo" ]; then


                  Should work.






                  share|improve this answer






















                    up vote
                    5
                    down vote



                    accepted







                    up vote
                    5
                    down vote



                    accepted






                    Is your intent to execute the script only on log-out?

                    From man bash:




                    When a login shell exits, bash reads and executes commands from the file ~/.bash_logout, if it exists.




                    Only on exit of a bash login shell.




                    If you intend to run an script on every shell close, use trap (see man bash):



                    trap /u1/myuser/on_exit_script.sh EXIT


                    Add it to .bashrc or some other that works for you.




                    Also, as the script will execute on exit, the $PWD will be the one active on exit, which may or may not be the same as when the shell was started. If you need to do something on the $PWD that was in use on exit, then yes, this test:



                    if [ "$PWD" = "$HOME/libfoo" ]; then


                    Should work.






                    share|improve this answer












                    Is your intent to execute the script only on log-out?

                    From man bash:




                    When a login shell exits, bash reads and executes commands from the file ~/.bash_logout, if it exists.




                    Only on exit of a bash login shell.




                    If you intend to run an script on every shell close, use trap (see man bash):



                    trap /u1/myuser/on_exit_script.sh EXIT


                    Add it to .bashrc or some other that works for you.




                    Also, as the script will execute on exit, the $PWD will be the one active on exit, which may or may not be the same as when the shell was started. If you need to do something on the $PWD that was in use on exit, then yes, this test:



                    if [ "$PWD" = "$HOME/libfoo" ]; then


                    Should work.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Feb 18 '16 at 10:54







                    user79743





























                        up vote
                        0
                        down vote













                        I like many of my shell scripts portable and shell-agnostic, so I prefer trapping EXIT over using the bash-specific .bash_logout method. And for a truly self-contained solution it's best not having to modify or create .bash_logout, nor rely on a separate script to be run on exit. So for minimal impact on foreign systems, my preferred method is to drop a custom login profile in place whenever I start working on a system, and having that same profile clean up itself and any helper scripts or config files it creates. I create a shell function "bye" in the script that performs said cleanup, and register it for auto-cleanup on logout, whether intentional or by simple disconnection, via:



                        trap bye EXIT




                        share








                        New contributor




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





















                          up vote
                          0
                          down vote













                          I like many of my shell scripts portable and shell-agnostic, so I prefer trapping EXIT over using the bash-specific .bash_logout method. And for a truly self-contained solution it's best not having to modify or create .bash_logout, nor rely on a separate script to be run on exit. So for minimal impact on foreign systems, my preferred method is to drop a custom login profile in place whenever I start working on a system, and having that same profile clean up itself and any helper scripts or config files it creates. I create a shell function "bye" in the script that performs said cleanup, and register it for auto-cleanup on logout, whether intentional or by simple disconnection, via:



                          trap bye EXIT




                          share








                          New contributor




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



















                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            I like many of my shell scripts portable and shell-agnostic, so I prefer trapping EXIT over using the bash-specific .bash_logout method. And for a truly self-contained solution it's best not having to modify or create .bash_logout, nor rely on a separate script to be run on exit. So for minimal impact on foreign systems, my preferred method is to drop a custom login profile in place whenever I start working on a system, and having that same profile clean up itself and any helper scripts or config files it creates. I create a shell function "bye" in the script that performs said cleanup, and register it for auto-cleanup on logout, whether intentional or by simple disconnection, via:



                            trap bye EXIT




                            share








                            New contributor




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









                            I like many of my shell scripts portable and shell-agnostic, so I prefer trapping EXIT over using the bash-specific .bash_logout method. And for a truly self-contained solution it's best not having to modify or create .bash_logout, nor rely on a separate script to be run on exit. So for minimal impact on foreign systems, my preferred method is to drop a custom login profile in place whenever I start working on a system, and having that same profile clean up itself and any helper scripts or config files it creates. I create a shell function "bye" in the script that performs said cleanup, and register it for auto-cleanup on logout, whether intentional or by simple disconnection, via:



                            trap bye EXIT





                            share








                            New contributor




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








                            share


                            share






                            New contributor




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









                            answered 4 mins ago









                            volkerk

                            11




                            11




                            New contributor




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





                            New contributor





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






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




















                                up vote
                                -1
                                down vote













                                I'm not sure how to do that through a bash_logout, though you could write a little script called exit that you'd put in /usr/local/bin/ with the kind of code that you put in your question, so that when you type exit in your console it'd do what you want if you are in the specific directory






                                share|improve this answer
























                                  up vote
                                  -1
                                  down vote













                                  I'm not sure how to do that through a bash_logout, though you could write a little script called exit that you'd put in /usr/local/bin/ with the kind of code that you put in your question, so that when you type exit in your console it'd do what you want if you are in the specific directory






                                  share|improve this answer






















                                    up vote
                                    -1
                                    down vote










                                    up vote
                                    -1
                                    down vote









                                    I'm not sure how to do that through a bash_logout, though you could write a little script called exit that you'd put in /usr/local/bin/ with the kind of code that you put in your question, so that when you type exit in your console it'd do what you want if you are in the specific directory






                                    share|improve this answer












                                    I'm not sure how to do that through a bash_logout, though you could write a little script called exit that you'd put in /usr/local/bin/ with the kind of code that you put in your question, so that when you type exit in your console it'd do what you want if you are in the specific directory







                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Feb 18 '16 at 10:19









                                    Archelio

                                    42




                                    42



























                                         

                                        draft saved


                                        draft discarded















































                                         


                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function ()
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f264019%2fexecute-code-in-a-logout-script-if-exiting-from-a-particular-directory%23new-answer', 'question_page');

                                        );

                                        Post as a guest













































































                                        Popular posts from this blog

                                        How to check contact read email or not when send email to Individual?

                                        Bahrain

                                        Postfix configuration issue with fips on centos 7; mailgun relay