LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 09-02-2004, 01:57 PM   #1
theta
LQ Newbie
 
Registered: Jan 2004
Posts: 4

Rep: Reputation: 0
Setting environment variables from shell script


Hi all,

I have a usb flash jumpdrive that I use to carry all of my current projects. What I'm trying to do is write a shell script for it that sets up several environment variables that are needed by my development environemtn such as CVSROOT, Java dependencies, Editors, etc. The problem is that I can't seem to get the variables to export.

For example:

## Old $CVSROOT
eos% echo $CVSROOT
<MY_EMAILADDRESS>:/home/cvs/cvs
eos% echo $JALOPY_HOME
JALOPY_HOME: Undefined variable.

eos% sh envsetup.sh
Initializing Environment
Setting CVS Environment: CVSROOT=:local:/mnt/diskonkey/src
Setting Jalopy Home: /mnt/diskonkey/Apps/jalopy-ant-0.6.2/lib

## After running envsetup.sh
eos% echo $CVSROOT
<MY_EMAILADDRESS>:/home/cvs/cvs
eos% echo $JALOPY_HOME
JALOPY_HOME: Undefined variable.

As you can see its not changing. Here is the script:

#! /bin/sh
echo "Initializing Environment"
CVSROOT=:local:$PWD/src ; export CVSROOT ; echo "Setting CVS Environment: CVSROOT=${CVSROOT}"
JALOPY_HOME=$PWD/Apps/jalopy-ant-0.6.2/lib ; export JALOPY_HOME; echo "Setting Jalopy Home: ${JALOPY_HOME}"

What am I missing? I'm not doing anything different that what some of my start up scripts do so how is running them from a console window any different?

Thanks for any help,
Joel
 
Old 09-02-2004, 02:38 PM   #2
mikshaw
LQ Addict
 
Registered: Dec 2003
Location: Maine, USA
Distribution: Slackware/SuSE/DSL
Posts: 1,320

Rep: Reputation: 45
You're running a script, then testing the variables from the current shell, yes?
When that script ends, the shell which ran the script also closes, and you're back to your old environment.
Any application which needs to use those variables needs to be spawned from the same shell which created the variables, or a child of that shell.

To test this, add the line "xterm &" at the end of your script.
Then test your environment from that xterm. You'll probably see the correct values.

Another option...
instead of executing the script (which opens a new shell), source it from your current shell:
source envsetup.sh
or
. envsetup.sh

Last edited by mikshaw; 09-02-2004 at 02:41 PM.
 
Old 09-02-2004, 03:02 PM   #3
theta
LQ Newbie
 
Registered: Jan 2004
Posts: 4

Original Poster
Rep: Reputation: 0
Quote:
You're running a script, then testing the variables from the current shell, yes?
Thats right.

Quote:
When that script ends, the shell which ran the script also closes, and you're back to your old environment.
Any application which needs to use those variables needs to be spawned from the same shell which created the variables, or a child of that shell.
Oh, that explains it.

Quote:
To test this, add the line "xterm &" at the end of your script.
Then test your environment from that xterm. You'll probably see the correct values.
No that didn't quite work the way I had hoped. When the xterm window pops up, I check it with something like: echo $CVSROOT and I get my old CVSROOT not the new one that the shell supposed to have set.

The only way I was able to get the new variables to take is if I open a terminal window, change my shell to sh then source the file like you suggested:

bash-2.05b$ sh
sh-2.05b$ source envsetup.sh
sh-2.05b$ echo $CVSROOT
:local:/mnt/diskonkey/src ## Correct new CVSROOT

This is fine but hoping there is a bit smoother solution. I like the idea of a new xterm window poping up ready to go. That way I can start the script by double clicking on the icon in a file explorer and a new window pops up with my environment ready to go.

Any ideas of things I might of missed?

Thanks
Joel

Last edited by theta; 09-02-2004 at 03:28 PM.
 
Old 09-02-2004, 04:15 PM   #4
mikshaw
LQ Addict
 
Registered: Dec 2003
Location: Maine, USA
Distribution: Slackware/SuSE/DSL
Posts: 1,320

Rep: Reputation: 45
That's weird....I copied and pasted your script exactly as you have it here, added xterm, and the variables work fine for me.
You're sure you typed "echo $CVSROOT" in the new xterm window rather than the original shell window?
 
Old 09-02-2004, 06:53 PM   #5
theta
LQ Newbie
 
Registered: Jan 2004
Posts: 4

Original Poster
Rep: Reputation: 0
Thats right I typed it in the XTERM window as opposed to the shell window. It is rather weird.

What I didn't mention before was that this is a unversity computer that I've been trying to get this to work on. Once I got home, it worked just like I hoped -- plugged in the usb stick, an icon popped up on my desktop, I double clicked on envsetup.sh, xterm came up, and all of my environment variables were initialized and ready to go. When I'm on campus again I'll check it and see if I can get it to work properly.

Joel
 
Old 09-02-2004, 08:50 PM   #6
mikshaw
LQ Addict
 
Registered: Dec 2003
Location: Maine, USA
Distribution: Slackware/SuSE/DSL
Posts: 1,320

Rep: Reputation: 45
It's possible that the machine has bashrc setting some environment variables...i don't know if this would be an issue here.
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Environment variables setting... simjii SUSE / openSUSE 4 11-19-2005 07:50 PM
Need help setting environment variables. cottonmouth Linux - Newbie 3 12-09-2004 04:54 PM
Sub shell environment variables tnine9 Programming 1 06-08-2004 01:27 PM
Setting environment variables from a script... sylvain_gnu Linux - Newbie 5 04-20-2004 12:31 PM
setting environment variables rezza Linux - Distributions 2 04-09-2003 08:21 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 08:05 AM.

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