Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Setting ENV variables in command shell

Status
Not open for further replies.

Tve

Programmer
May 22, 2000
166
FR
Hi,

I'm trying to read ENV varaibles from an ini file and set these variables in the cmd.exe.

Unfortunatly, when I set ENV variables, they are only set in the perl process shell and not in the parent shell.

Does anybody know how to resolve this?


Thierry

 
Well, the problem is when you run a perl script, it runs as a child process of the shell you call it from, so changes to the child don't take place in the parent. The only way that I would know of would be to actually add them to the .bash_profile or .mycshrc file, so they are automatically set, but that's kind of a permanant thing, and still doesen't work for your problem, because you have to restart the shell.

You may be able to fake it with an exec or system or backtick call to a shell with setenv...., but this may create yet another sub-shell. You'll have to read the docs to see if one of these calls actually uses the shell you are running the script from. I hope that makes sense, if not, let me know and I"ll explain further. As always, I hope that helped!

Disclaimer:
Beware: Studies have shown that research causes cancer in lab rats.
 
This all makes sense....


Thanks for the shot.


Thierry
 
You could also try exporting the environment variables after you set them. This works on some *nix systems, not sure which. Export is an *nix command, so check the man pages for your system.
Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
No, this will still not work, please read my previous post, I believe it explains why.

Disclaimer:
Beware: Studies have shown that research causes cancer in lab rats.
 
Actually, it doesn't explain why. Isn't the purpose of the export command to export environment variables from a sub-shell to the main shell? Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Errrmmm :) no tsdragon, it isn't :)

when you say something like this in a script

export MYVAR

That means that when THAT SCRIPT starts a subshell the variable MYVAR will be visible to the subshell.

It does not mean that the program that called your script will able to see MYVAR.

You can't, as far as I know, do what the OP wanted. However.... You *can* do this:
[tt]
$ENV{'var_one'} = 'hello';
$ENV{'var_two'} = 'goodbye';
system('some_command');
[/tt]

the program 'some_command' will then have access to the variables var_one and var_two. Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
What I meant was to use the system command to set and export the environment variables. Use the semicolon, or whatever your flavor of *nix uses to separate commands and put the set commands and the export commands in the same command line. It's worth a try.
Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Have a go ts, don't *think* that will work -- here's why I think so.

The system() function starts a subshell, a child process. This child process only last as long as the system() function lasts and, when it finishes, any variables it sets or changes will disappear unless it leaves child processes of its own.

I like your sig by the way <smile> Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
Thanks, I borrowed it from somewhere, and changed catsup to mustard (I hate catsup).

I know system starts a child process (or subshell), but my understanding is that the export command is supposed to &quot;export&quot; the named environment variable(s) from the subshell to the &quot;main&quot; shell environment. Tomorrow morning I'll check the man page for export, and maybe even try it out from perl to see how it works.
Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
catsup..... sounds horrible, if I knew what it was I'm sure I'd hate it.... Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
On SunOS 5.8 - man export gives,

&quot;export ...... functions to determine the characteristics for the environmental variables of the current shell and its descendents.&quot;

BTW - Is it dangerous to stand behind a dragon? Seems like it might be if he has had to much mustard? <yuk-yuk-yuk> Or, is that how they fly????
.... Sorry, I'm a connoiseur of poor humor.


keep the rudder amid ship and beware the odd typo
 
That description from the SunOS man page isn't real helpful, but since it says &quot;descendents&quot; it may not export to the &quot;main&quot; shell. I'll have to try it out and see what happens.

Now that I've post ON topic...

Catsup is an alternative spelling of ketchup. You know, that nasty tomato and water stuff that Ronald Reagan thought should qualify as a vegetable in school lunches.

It's only dangerous to stand behind a dragon if he has been eating hot mustard. Salsa, jalapenos and Tabasco sauce are even more dangerous. (In dragon talk we call those &quot;jet fuel&quot;.) :)
Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top