Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
06-03-2009, 06:41 PM
|
#1
|
Member
Registered: Jul 2007
Location: Scunthorpe, UK
Distribution: Ubuntu 8.10; Gentoo; Debian Lenny
Posts: 961
Rep:
|
echo $USER/ sudo echo $USER
Could anybody explain why in a script run as sudo, echo $USER will return root, but sudo echo $USER will always return the username running sudo. For a more clear question:
Code:
jc@jcmain:~$ sudo echo $USER
jc
jc@jcmain:~$ sudo ./sudo_demo.sh
root
jc@jcmain:~$ cat sudo_demo.sh
#!/usr/bin/env bash
echo $USER
I essentially need the code running as sudo to return 'jc', checking the wd is not viable here, either. Ideas?
Last edited by jamescondron; 06-03-2009 at 06:42 PM.
Reason: missed a sudo
|
|
|
06-03-2009, 06:53 PM
|
#2
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,516
|
well $USER is set by the shell.
I reckon...
the line is evaluated first by the shell and all the variables
interpolated, then the command is forked off to execute
so:
sudo echo $USER
runs as:
sudo echo jc
it happens before the sudo
|
|
|
06-03-2009, 07:05 PM
|
#3
|
Member
Registered: Jul 2007
Location: Scunthorpe, UK
Distribution: Ubuntu 8.10; Gentoo; Debian Lenny
Posts: 961
Original Poster
Rep:
|
That actually sounds reasonable put that way, I really hadn't thought it like that. Ah well, I may just script in an extra arg to handle this stuff instead, feels messier, but nevermind
|
|
|
06-03-2009, 07:06 PM
|
#4
|
Member
Registered: Dec 2007
Location: Slightly left of center
Distribution: slackware
Posts: 276
Rep:
|
The difference is because you are telling sudo to apply the privilege elevation to the echo command in the first case, and the script in the second case.
The echo command is running as root, but it is reporting the current shell user. If you were to modify the code for echo to report its user, it would be root.
I don't know of any elegant method to get the invoker's UID or LOGNAME. I'd probably use a brute-force method of some sort. Hopefully someone else here may know a better trick.
edit: heh, bigearsbilly beat me to it ;-)
Last edited by raconteur; 06-03-2009 at 07:08 PM.
|
|
|
06-03-2009, 07:28 PM
|
#5
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,516
|
maybe try the id command?
|
|
|
06-03-2009, 10:12 PM
|
#6
|
Member
Registered: Jul 2007
Location: Scunthorpe, UK
Distribution: Ubuntu 8.10; Gentoo; Debian Lenny
Posts: 961
Original Poster
Rep:
|
id does exactly the same thing when called in a script run as sudo
|
|
|
06-03-2009, 11:58 PM
|
#7
|
Senior Member
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,837
|
Code:
$ man sudo
...
ENVIRONMENT
...
SUDO_USER Set to the login of the user who invoked sudo
|
|
|
All times are GMT -5. The time now is 01:34 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|