tags:

views:

96

answers:

3

Hello,

If I create the script /root/bin/whoami.sh conatining:

#!/bin/bash
whoami

And this script is called by a user with a properly configured sudo, it will indicate

root

Is there a fast way to obtain the actual user in a script, or will I have to resort to parameters passing along this username?

TIA, Bert

+9  A: 

I think $SUDO_USER is valid.

#!/bin/bash
echo $SUDO_USER
whoami
Brandon Horsley
Strange thing: `sudo env` shows `SUDO_USER` but `sudo echo $SUDO_USER` prints nothing...
Job
@Job when calling sudo? Because it has no sense otherwise...
Enrico Carlesso
job, this is not strange, it's expected. in `sudo echo $SUDO_USER`, bash is evaluating $SUDO_USER **before** executing sudo. try the script posted in this solution, it works.
@evilclown: Yes you're right, didn't think about that.
Job
Thanks Brandon!
quadmore
@quadmore: If you're happy with the answer, don't forget to accept it.
Job
A: 

Odd, the system does distinguish between real and effective UIDs, but I can find no program that exports this at shell level.

msw
A: 

try:
id -urn

frankc