Skip to content

ways to get find who owns STDIN, and then get their $HOME directory

DeeNewcum edited this page Sep 4, 2012 · 3 revisions

There are lots of ways to do this. I'm documenting some of them here, in case only one of them works on a particular OS.

A)
STDIN_OWNERS_HOME=$(perl -MPOSIX -e'print((getpwuid((stat ttyname 0)[4]))[7])')

B)
STDIN_OWNERS_HOME=$(perl -e'print((getpwuid((stat shift)[4]))[7])' $(tty))

C)
STDIN_OWNERS_HOME=$(eval echo ~$(who am i | cut -d ' ' -f 1))

D)
STDIN_OWNERS_HOME=$(eval echo ~$(logname))

Test these after sudoing to root. If it works properly, it should show who the original user is.

Alternate ways of getting the same information include the effective-UID (only when running a setuid script), or $SUDO_USER (only when using 'sudo').

Places where the above don't work:

  • under tmux, C and D don't work (A and B do work)
Clone this wiki locally