469,338 Members | 8,284 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,338 developers. It's quick & easy.

Adding ENV variables in linux

I was writing a perl program where I have to add one environment variable.
like RCS_ROOT.
I tried to use system( " setenv RCS_ROOR ****");
i also tried system(" export RCS_ROOT=********");
But nothing is working .. when i run it tell no file called setenv.
Please let me know how to set userdefined env. using perl.
Basic idea is set some user defined paths with setup script
and later use those paths in diff. scripts to locate files.

please help

regards
vijay
Feb 27 '07 #1
8 10746
arne
315 Expert 100+
You may want to have a look at the %ENV hash. In this hash Perl maintains the environment variables.
Feb 27 '07 #2
KevinADC
4,059 Expert 2GB
I'm pretty sure he got the answer on another forum.
Feb 27 '07 #3
No I have not found any answer!!
well I tried to manipulate %ENV but %ENV contains list of only existing variables
but I want to add new variable which does not exist...
Feb 28 '07 #4
KevinADC
4,059 Expert 2GB
maybe:

$ENV{RCS_ROOT} = "your value here";

only works for the duration of the script though. If you need it to be persistent write it to a file and read the file each time the script is envoked. Maybe somone else knows a better way.
Feb 28 '07 #5
thanks kevin this will solve the problem but still my doubt is why system does not execute the setenv?
I tried to write shell script and then execute the script even with that I am not able to see the variable added or set !!!!!
well I wil try to do by ur method.....


thanks

maybe:

$ENV{RCS_ROOT} = "your value here";

only works for the duration of the script though. If you need it to be persistent write it to a file and read the file each time the script is envoked. Maybe somone else knows a better way.
Feb 28 '07 #6
miller
1,089 Expert 1GB
First off, it sounds like you should simply create a file to save these settings in. That is the best way to ensure that the settings are persistant. Then you simply have any of your subsequent scripts import the data. You could even still use the $ENV variables method.

For example, say that this was your settings file. ".settings"
Expand|Select|Wrap|Line Numbers
  1. $ENV{RCS_ROOR} = "your value here";
  2.  
Then all scripts that needed this value could simply import it by doing an eval or require. Then simply access the data using the environment variable.

Expand|Select|Wrap|Line Numbers
  1. require ".settings";
  2.  
  3. print "$ENV{RCS_ROOR}";
  4.  
If you do not need persistance, then making a simple assignment like in Kevin's example would work fine.

Concerning why setenv and export do not work. This is something that I've attempted before and eventually abandoned as a bad approach. Basically, my understanding is that setenv and export are something that is specific to the shell environment thatyou are using, whether it's tsh or bash, etc. You could try to do a system("bash export foo='bar'") call, but that would only last for that one statement. The only way to effect all subsequent shell environments is to add the export call to the .bashrc or .tcshrc start routine. And even then, that's not going to effect any shell environments that are already spawned.

Anyway, the moral of the story is to not rely on the environment that you're running perl under to set configuration data for other perl scripts. Instead create a configuration directory or file that all the scripts can share. Or if you simply need it for one session, use the method that Kevin suggests.

- Miller
Feb 28 '07 #7
Thank you Miller for help
I dropped Idea of setting environment variables with perl script because I dont want setup script to be dependent on any other file or any other scripts to be dependent on other files. ( that was the reason I wanted to set env. variables )
Now for setup I am using shell scripting.
Like we source .bash or .cshrc when we login we can source this setup file everytime we login. and everything is working fine with this setup.


thanks again
vijay
Mar 2 '07 #8
miller
1,089 Expert 1GB
That can work, and is pretty much the only way to do it using ENV variables. I would simply like to point out that you are still dependent on an external file, be it somewhat transparently.

Anyway, glad you found a solution that you're happy with.

- Miller
Mar 2 '07 #9

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by surrealtrauma | last post: by
1 post views Thread by John Ratliff | last post: by
115 posts views Thread by Mark Shelor | last post: by
5 posts views Thread by Daz | last post: by
18 posts views Thread by MajorSetback | last post: by
25 posts views Thread by sidd | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.