LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-12-2008, 10:28 AM   #1
bcarson
LQ Newbie
 
Registered: Oct 2008
Posts: 3

Rep: Reputation: 0
Child can't see parent's environment variable


I'm a Linux newbie, so I'm probably overlooking something obvious. I'm running Ubuntu 8.04 and trying to send updates to a SOLR server via a bash script.

Problem is, when I call the import script it gives me the error message:
You need to set the SOLR_HOME environmental variable before running this script.

Now, I can set SOLR_HOME using the export command, but the bash script still can't see it. The code in the bash script generating the error is this:
if [ -z "$SOLR_HOME" ]
then
echo "You need to set the SOLR_HOME ... "
fi

Strangely, the import script does run (without warnings) if I call it using a Perl script with the line:
`export SOLR_HOME=[directory];./import.sh`

So: Why would a child process not see an environment variable exported by its parent process? And why would commands issued from the command line fail while the same commands sent from a Perl script succeed?
 
Old 10-12-2008, 11:53 AM   #2
clvic
Member
 
Registered: Feb 2008
Location: Rome, Italy
Distribution: OpenSuSE 11.x, vectorlinux, slax, Sabayon
Posts: 206
Blog Entries: 2

Rep: Reputation: 45
How do you call the import script? Have you tried typing "echo $SOLR_HOME" in bash just after you set up its value?
 
Old 10-12-2008, 02:22 PM   #3
bcarson
LQ Newbie
 
Registered: Oct 2008
Posts: 3

Original Poster
Rep: Reputation: 0
From the bash shell I type:

export SOLR_HOME=[directory]

After that, I can enter
echo $SOLR_HOME
and see the value I've just established; or I can enter env and see SOLR_HOME in the list. The script warns me to set the SOLR_HOME environment variable anyway.

I run the import script with the command:
sudo ./import.sh

the same command without "sudo" doesn't work: the shell just sits for a minute and then gives me another prompt. No warnings about permissions or anything. This is also puzzling behavior, but I'm not sure it's related.
 
Old 10-13-2008, 03:12 AM   #4
clvic
Member
 
Registered: Feb 2008
Location: Rome, Italy
Distribution: OpenSuSE 11.x, vectorlinux, slax, Sabayon
Posts: 206
Blog Entries: 2

Rep: Reputation: 45
So, that's the point: the "sudo" command does not preserve the environment, that is, it does not preserve variables values.
Give a look at the sudo manpage, the -E option and "SECURITY NOTES" describe how to deal with this. You can change your sudoers file to preserve the environment, or you can insert your environment modifications in a script, say do_all.sh, that called with 'sudo do_all.sh' changes the environment and eventually calls ./import.sh
 
Old 10-13-2008, 08:37 AM   #5
bcarson
LQ Newbie
 
Registered: Oct 2008
Posts: 3

Original Poster
Rep: Reputation: 0
I suspected I was overlooking something obvious. Thanks!
 
Old 10-13-2008, 09:47 AM   #6
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
You can run the script like this:
SOLR_HOME=[directory] ./import.sh

You can also use "env"
env SOLR_HOME=[directory] ./import.sh

If a variable is exported in a script, you need to source the script to retain the values.

Last edited by jschiwal; 10-13-2008 at 09:50 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Environment Variable linux_linux Linux - Newbie 4 03-20-2008 10:16 PM
how to retain changes made in a global variable by a child process after in exits sagsriv Linux - Newbie 3 03-12-2008 12:44 AM
How to share the same variable between parent and child process? zwylinux Programming 6 03-26-2007 02:47 AM
How to change environment variables of a parent envrionment from a child one. pulsez Linux - Software 1 11-09-2005 06:24 AM
GCC fork() - Can a child change a parent variable? johnhardey Programming 4 03-10-2005 10:49 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:19 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration