Re: Problems with wincfg

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Tue, 2012-06-19 at 13:59 -0500, David Bailey wrote:
> I'm having difficulty getting my password back for the bugs website, but I wanted to add that testing for USER == root doesn't seem to handle sudo:
> 
> sudo echo $USER
> 
> returns 'dave' for me.
> 
This depends on when $USER is evaluated and (in a bash session) when the 
value is substituted for the variable name. In your example substitution 
will occur when bash parses the command line, i.e. 'dave' was substituted 
for $USER before sudo was executed. OTOH, if the substitution done is inside
a simple script which is run under the control of sudo, then the substitution 
takes place within the environment established by sudo. Here you go:

$ cat test
#!/bin/bash
echo $USER
$ ./test
kiwi
$ sudo ./test
root
$ 

What happens is that this time no substitution happens when the command
is parsed. Instead, sudo changes user to root and spawns a shell under
that user. This shell executes the script by spawning a second shell (by
executing #!/bin/bash as 'root') and this evaluates $USER and does the
substitution. 

Of course, much the same applies if the value of USER is retrieved
within a C program by calling getenv(): again, its a delayed evaluation
that would happen after sudo has done its thing. Finally, take a look at
the next version of my script and think about what it does:

$ cat ./test
#!/bin/bash
if [ -n "$1" ]
then
	echo $1
else
	echo $USER
fi
echo "Local USER is $USER"
$ sudo ./test $USER
kiwi
Local USER is root
$ sudo ./test 
root
Local USER is root


Martin





[Index of Archives]     [Gimp for Windows]     [Red Hat]     [Samba]     [Yosemite Camping]     [Graphics Cards]     [Wine Home]

  Powered by Linux