Get current logged in user from a script run as root.

Ok, so, in order to install some dependencies of a program I made, a script has to be run as root. The thing is that I have to copy some things into the home folder of currently logged in user, but the variable $HOME returns '/root' and the $USER returns 'root' :frowning:
Is there any way to see who is currently logged in?:confused::confused::confused:

1) What Operating System and Shell are you using?
2) Did you log in as the user before becoming root?
3) How did you gain root permissions? (su , sudo ?)
4) What reply do you get for:

who am i

5) I'm assumig that this is not a single-user workstation.

1)Ubuntu 10.04 bash
2)yes
3)sudo su
4)In the command 'who am i' I get as a response

alex     pts/0        2010-08-17 17:34 (:0.0)

but I am running in a single user machine. I dont know the answer if there were more than one users in my system ( 5) )

---------- Post updated at 01:30 PM ---------- Previous update was at 09:38 AM ----------

I told you what I have! Any other reply?:confused::confused:

Thanks for your reply :slight_smile:

So, I have to tell the users to use su -m to become root and then run the script??? It's quite confusing don't you think?

Is there any way to access the currently logged on user when being root?

try logname

Ouaoooo this solves everything!!! Thank you very much!!!

Good answer. "logname" returns the same user as "who am i" but is easier to work with.

Yes, it was exactly what I wanted!