5

This command is in my .bash_logout script to delete a temporary directory when I logout after running some processing:

/bin/rm -rf /tmp/tmpdir/

However, I usually have more than one connection to a machine and I'd like to have this executed only if it is the last connection to logout. Is it possible to check this and put and if statement in?

1 Answer 1

4

I'm going to post what I came up with and if anyone has any comments or suggestions, please let me know.

i=$(who | grep -c "username")

if [ $i -eq "1" ]
then
/bin/rm -rf /tmp/tmpdir/
fi
2
  • 2
    Not bad, but I encourage you to get into the habit of quoting all shell variable references (e.g., "$i") unless you have a very good reason not to. Conversely, you don't really need to quote simple, one-word strings (like 1 and your username), but, of course, it (usually) doesn't hurt, and is, of course, necessary for strings that contain whitespace or other special characters. Oct 1, 2014 at 21:24
  • To make this a template for any user, I use $(whoami) instead of "username".
    – emk2203
    Oct 16, 2020 at 3:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .