Operating System - HP-UX
1828892 Members
2725 Online
109985 Solutions
New Discussion

Re: Something about shell scripting.

 
Kenny Chau
Trusted Contributor

Something about shell scripting.

Hi, I had a script to export some useful variable for other script to run. However, when I used "su - $account -c "script" " to run some scripts, the exported variable had gone. How to retain the export variable after I used "su - $account -c "script" " on other scripts?

Thanks.
Kenny
12 REPLIES 12
Patrick Chim
Trusted Contributor

Re: Something about shell scripting.

Hi,

The "script" should be a absolute path of your environment variable script and NOT a variable because it cannot pass through to a 'su -c' command.

Regards,
Patrick
Kenny Chau
Trusted Contributor

Re: Something about shell scripting.

Thanks for your reply. I had tried this in other machines (L4000) and it is succeeded. However, when I put the same script in the A400 machine, it did not work. Do you have any clue?
Kenny
Andreas Voss
Honored Contributor

Re: Something about shell scripting.

Hi,

what you could do is writing your variables into a file before su-ing.

echo "export VAR='$VAR'" >/tmp/set_variables
echo "export VAR1='$VAR1'" >>/tmp/set_variables
and so on ...
chown $account /tmp/set_variables

Then in the script of $account add the line
. /tmp/set_variables

Hope this helps
Kenny Chau
Trusted Contributor

Re: Something about shell scripting.

Thanks Andreas, I know it is worked. However, I'm just wondering why the same scripts work in other machines but not the one that I am using. Same scripts, same variable files but work on other machines. Do you know anything that I had missed for this?

Thanks.
Kenny
Patrick Chim
Trusted Contributor

Re: Something about shell scripting.

Hi,

I think it's the shell problem because I encounter it before. Sometime you call a ksh script but it internal call another sh script will cause you not to get any environment variable which specific in the sh script.

Regards,
Patrick
Carlos Fernandez Riera
Honored Contributor

Re: Something about shell scripting.

It is not very problable that using a su -command will pass exported variables.

See inside your .profile files, and other involved files such as $HOME/.chsrc, .kshrc and /etc/profile for that variables.
unsupported
Kenny Chau
Trusted Contributor

Re: Something about shell scripting.

So you means that I can not pass exported variables after I use the su - command?? But why the same scripts in my other machines will work?? Do you have any clue about this??

Thanks.
Kenny
Carlos Fernandez Riera
Honored Contributor

Re: Something about shell scripting.

I mean that these suposed exported variables are set in the user environment, not from original shell.

Create a file /tmp/jj

echo JJ="-"$JJ"-"

export JJ in the original shell
JJ="OK"
export JJ

and now

su - user -c "/tmp/jj"....

unsupported
Wodisch
Honored Contributor

Re: Something about shell scripting.

Hello Kenny,

since only a process can modify its own environment,
you could do something like:

su - USER -c "ENV=$HOME/.special-vars sh "

where "$HOME/.sprecial-vars" would be shell-script
(with permissions "r" and "x") containing all the
"export" statements and variable assignments you
need...
Since Korn-Shell and POSIX-Shell both do execute the
script referred to in "ENV" before showing the prompt,
that would do what yout want.

OR, you may have a line in your "$HOME/.profile" like
this:

if [ -r $HOME/.variables ]; then . $HOME/.variables; fi

Hence, if there is a script ".variables" it will be sourced
into your login shells (and "su -", too). Then you will just
have to create or modify that script...

Just my ?0.02,
Wodisch
Thomas Boerner_1
New Member

Re: Something about shell scripting.

Hi,

at least on Linux there are two slightly different ways to submit the su command:

su $account
or
su - $account

The difference is that with "-" (means login) there will be a completely new environment whereas without the old environment will be used. I tried that:

tboerner@N01049LUX:~/Download > export MY_TEST_VARIABLE="Some dummy value"
tboerner@N01049LUX:~/Download > echo $MY_TEST_VARIABLE
Some dummy value
tboerner@N01049LUX:~/Download > su - tb
Password:

tb@N01049LUX:~ > echo $MY_TEST_VARIABLE

tb@N01049LUX:~ > logout
tboerner@N01049LUX:~/Download > echo $MY_TEST_VARIABLE
Some dummy value
tboerner@N01049LUX:~/Download > su tb
Password:
tb@N01049LUX:/home/nokia/Download > echo $MY_TEST_VARIABLE
Some dummy value
tb@N01049LUX:/home/nokia/Download > exit
tboerner@N01049LUX:~/Download > echo $MY_TEST_VARIABLE
Some dummy value

Hope that helps a bit to explain your question.
Have a nice day
W.C. Epperson
Trusted Contributor

Re: Something about shell scripting.

Kenny sed: "So you means that I can not pass exported variables after I use the su - command?? But why the same scripts in my other machines will work?? Do you have any clue about this??"
>>>
Kenny, apparently on the machines where things work, the environmental vars you need are being set globally at the start of each user shell (like from /etc/profile). Compare the /etc/profiles from a system where things work and from a system where things are broken, and you'll probably see what's happening.

"I have great faith in fools; self-confidence, my friends call it." --Poe
Klaus Crusius
Trusted Contributor

Re: Something about shell scripting.


I agree with Thomas, there is a difference wether you are using the "-" or not.
This is clearly documented on the man page of su. Unfortunately su has been withdrawn from the XPG and POSIX standards. That could explain diverging implementations on different OS. There is no difference between HPUX10 and HPUX11 with respect to the su man page.

Klaus
There is a live before death!