Re: Environment variables ignored in shells forked within a su shell

From: Bill Thompson (bill.thompson@GOODYEAR.COM)
Date: Wed Feb 26 2003 - 11:22:35 EST


Jorge,

I've seen some good replies to this post however none of these should be
necessary. There should be no problem with your original syntax.

I tried something similar on an AIX 4.3.3 system.

First I created a file called "/tmp/new_env" that looked like this:

- - - - - snip - - - - -
export PATH="/bin:/dummy"
export NEWVAR="this is a new variable"
- - - - - snip - - - - -

Then I created a second file called "/tmp/test_script" that looked like
this:

- - - - - snip - - - - -
echo $PATH
echo $NEWVAR
- - - - - snip - - - - -

I then ran the following command:

su user -c ". /tmp/new_env;sh /tmp/test_script"

The results were what I expected:

- - - - - snip - - - - -
/bin:/dummy
this is a new variable
- - - - - snip - - - - -

Ergo, the script IS able to see the new and changed variables.

In addition, because you did not use the "-" option, all of root's
environment is also defined.

(Note: I also tried it with the double quote before the -c (even though I
think this is a documentation error) and got the same results.)

The question is why isn't this working for you? And if this isn't working
my guess it the other solutions aren't going to make any difference.

Try this command and see if your environment is what you think it should
be:

su user -c ". alternate_profile; set" >/tmp/environment_for_user

HTH

Bill Thompson
Sr UNIX Systems Administrator
The Goodyear Tire & Rubber Co.

Contains Confidential and/or Proprietary Information
May Not Be Copied or Disseminated Without Express Consent of The Goodyear
Tire & Rubber Company.

AIX-L Archives: http://marc.theaimsgroup.com/?l=aix-l&r=1&w=2

 ----- Original Message -----
 From: Fernandez Garay Jorge
 Newsgroups: bit.listserv.aix-l
 To: aix-l@Princeton.EDU
 Sent: Tuesday, February 25, 2003 5:26 PM
 Subject: Environment variables ignored in shells forked within a su shell

 Within a sh program (is an automated process), I need to su from root

 to a user whose .profile is exessively customized for interactive
 sessions,

 while the automated process runs in the background.

 I've tried to avoid this .profile, creating a 'clean', alternate file,
 containing

 only the indispensable environment needed for the execution of the auto-

 mated process.

 Next, from this process, I've su'ed to user in the following way:

 Having root credentials

 su user "-c . alternate_profile; trigger_process". (Note: I'm not
 reading

 user's profile)

 Where alternate_profile is a file containing environment variables, all of

 them exported. Note this file is read, then every line in it, executed,
 as

 a real .profile.

 trigger_process is a process that correct some undesired conditions in

 the user's environment, and needs the exported variables to run OK.

 Well, problem is that this trigger process can't see the exported envi-

 ronment. Within the shell openned by 'su', variables are set.

 trigger_process runs in a new shell, and it happened as if all the
 exported

 environment gets deactivated within the new shell

 Has anyone in the list some idea to resolve this problem??

 Any help will be appreciated. Thanks in advance.

 Jorge

         Ing. Jorge A. Fernandez Garay

                  Administrador UNIX

 Jefatura de Tecnologia y Comunicaciones

                     Gerencia de Sistemas

                                              COTO CICSA



This archive was generated by hypermail 2.1.7 : Wed Apr 09 2008 - 22:16:37 EDT