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

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


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.

| improve this answer | |

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. And of course far worse would be modifying or overriding what the "exit" command itself does on the system - the solution should not affect anyone else!

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 then source that profile (using "." which works under ksh too), which sets up all my aliases etc. - but I don't want it sticking around when I'm done.

So I create a shell function "bye" in the script itself that performs said cleanup, and register it for auto-cleanup on logout, whether intentional or by simple disconnection, via:

trap bye EXIT

That ensures I'm not leaving ugly traces of my work behind. Which specific cleanup it should perform is up to you, the directory-specific "make clean" is just one of many possibilities. And once the logout has occurred via whatever means, all traces of the profile including even the cleanup function are gone.

You can go one step further - perhaps there's different types of systems on which you want different types of cleanup to occur, or on some systems you don't want this sort of cleanup to occur at all. You could build switches into the "bye" shell function, or make different ones "bye1", "bye2" and register the desired one based on some test, e.g. the existence of some file. That's how I prevent deletion of my custom profile on systems I use longer-term, where I want to keep the same profile around.

| 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

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy