3

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?

2

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
    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. – G-Man Oct 1 '14 at 21:24

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.