A momentary crisis of faith, and who am I anyway?

I find that I have these odd periods at the end of big projects. I wander around for a bit wondering what I am meant to do now that the burningly urgent things are off my todo list. This time normally ends up being playing with the kids, watching TV or being mopey.

I offer this little sequence as the blog form of a holding pattern until I know what I am going to work on next…

    The other day I discovered that I no longer know who I am all the time. Specifically, slack unsets the environment, which means that the whoami command no longer works when you are running a command as root. To whit:

    sudo bash
    root@machine:~# whoami
    root@machine:~# echo $SUDO_USER

    So, if SUDO_USER is unset, how do I determine who the command is running as? Well, my immediate thought was that /proc would have something, and in fact it does. Here is what I ended up with:

    root@machine:~# cat /proc/$$/environ | sed 's/[^[:print:]]/\n/g' | grep USERNAME

    Horrible, isn’t it? I must also admit that I had to get onto IRC and wait for Jaq to teach me about that :print: thing. Good on him for being nerdier than me.

    So, that’s still horrible. So horrible. What else can I do? Someone else in IRC land suggested:

    root@machine:~# ps auxeww | grep $$ | sed -e 's/^.*SUDO_USER=//' -e 's/ .*$//'

    Still ewwww. So what else is there? Well, one of the older geeks on the IRC channel suggested this:

    root@machine:~# who am i
    mikal    pts/16       Apr 15 15:38 (:0.0)
    root@machine:~# who -m
    mikal    pts/16       Apr 15 15:38 (:0.0)
    root@machine:~# who -m | awk '{print $1}'

    who am i is an alias for the -m option in who. And it’s just what I want. We have a winner!