How to return user name in a script even if run by SUDO?

Flynn, Steve (L & P - IT) Steve.Flynn at capita.co.uk
Tue Apr 7 12:14:28 UTC 2009


-----Original Message-----
From: ubuntu-users-bounces at lists.ubuntu.com
[mailto:ubuntu-users-bounces at lists.ubuntu.com] On Behalf Of Karl F.
Larsen
Sent: 07 April 2009 12:52
To: Ubuntu user technical support,not for general discussions
Subject: Re: How to return user name in a script even if run by SUDO?

Arch Willingham wrote:
> How do I return the name of the user in a script even if it is fired
off by SUDO? In other words, if my script has these two lines:
>
> echo ${USERNAME}
> echo ${USER}
>
> If I run it normally when logged in a user named wareagle, it prints
out:
>
>
> wareagle
> wareagle
>
> But if I run it via sudo, it prints out:
>
> root
> root
>
> I need to be able to know the name of the currently logged in user
even if the script is run by sudo?



Shell variables:

UID	The numeric real user id of the current user.
EUID	The numeric effective user id of the current user.

You will be able to grep these id's in the /etc/passwd file for the
username.




This email and any attachment are intended solely for the addressee, are strictly confidential and may be legally privileged. If you are not the intended recipient any reading, dissemination, copying or any other use or reliance is prohibited. If you have received this email in error please notify the sender immediately by email and then permanently delete the email. Copyright reserved.

All communications, incoming and outgoing, may be recorded and are monitored for legitimate business purposes. 

The security and reliability of email transmission cannot be guaranteed. It is the recipient’s responsibility to scan this e-mail and any attachment for the presence of viruses. 

The Capita Group plc and its subsidiaries ("Capita") exclude all liability for any loss or damage whatsoever arising or resulting from the receipt, use or transmission of this email. 

Any views or opinions expressed in this email are those of the author only.




More information about the ubuntu-users mailing list