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 John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

perl: setting ENV variable

Status
Not open for further replies.

robw6

Technical User
Nov 24, 2003
3
GB
I'm trying to set some ENV variables so that other programs can use them.
Setting via %ENV doesn't work after the Perl script (child) has terminated.
So I thought the Shell module should help - apparently this allows shell commands to be run.

Although something like this (example from perldoc):

#!/usr/bin/perl -w
use Shell;
$foo = echo("howdy", "funny", "world");
print $foo;

does indeed work,, I found that trying to "export" to bash doesn't:

#!/usr/bin/perl -w
use Shell;
$foo = export('WORLD=earth');

Perl reports:
Can't exec "export": No such file or directory at (eval 1) line 57.
Can't exec export: No such file or directory

Any ideas anyone?
Rob
 
Have you tried using the "system" command directly?

system("export('WORDL=earth')");

 
Thanks - just tried it with this result:

sh: -c: line 1: syntax error near unexpected token `'WORLD=earth''
sh: -c: line 1: `export('WORLD=earth')'

I guess this means that the shell is complaining - but does the "sh" mean that it's not bash?
I wonder if this is the reason for my problems - ie it's not being passed to the _bash_ shell?
Rob
 
Correct, your passing sh args to bash.

I can not say for certain but I do not know if this will work. Even if you export using the system command it will only export for that session.

Everytime you do a system call it opens a shell, issues your commands and then closes it.

So changing the ENV vars within that new shell will probably not change the system wide.
 
I think you're right - I found a tip on another list that answers a similar query, although that concerned a bash script rather than Perl.
It turns out that if you _run_ a bash script that does export VARIABLE then it does not persist - only works for children.
However if instead you _source_ the script then the export VARIABLE is persistent.

So I think this is a possible way to solve the problem (ie use a bash script) - but I'd rather use perl if possible (easy to interface to a gui etc)
 
Correct, but every session would have to source the script to get the new variables, they would not auto-populate into all sessions.

Since no matter what you do everyone will have to 'source' to get the changes you maybe should consider having your perl script update .bashrc instead. Since that gets sourced at every logon your users will get the new env vars.
 
Hi, how do I solve this :

I want to use a perl-skript to read every line of a config-file, then want to do following :

system("IFACE=$(cat regel1.tmp | cut -f2)");

but I'm always getting following error :

sh: -c: line 1: syntax error near unexpected token `-f2)'
sh: -c: line 1: `IFACE=0 65534 17 16 15 14 1 0cat regel1.tmp | cut -f2)'

Why ?

Thanks for any reply!
cu
bruno
 
bruno - in this context you need to put a \ before a $ sign in a double quoted string, otherwise Perl will try and do something clever with it

Mike

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

It's like this; even samurai have teddy bears, and even teddy bears get drunk.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top