Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

How do I set env. Variables from Perl with shell script

by Noame (Beadle)
on May 04, 2009 at 11:49 UTC ( [id://761703]=perlquestion: print w/replies, xml ) Need Help??

Noame has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I’ve Perl script which should execute shell script ‘setParam.sh’.
The shell script is initialization environment variables, like:

HOME=”/opt/include” INCLUDE="${HOME}/INC;${HOME}/INC/new”
Please advice, how can I use all the Variables that were set in ‘setParam.sh’ in the Perl script?
How can I expose the ‘INCLUDE’ variable to the Perl so he can use it, f.e:

#! /usr/local/bin/perl # Execute setParam.sh.... chdir $ENV{HOME}; print ("$ENV{INCLUDE}");
I must to execute the shell script from perl in runtime.

Thanks

Replies are listed 'Best First'.
Re: How do I set env. Variables from Perl with shell script
by moritz (Cardinal) on May 04, 2009 at 12:09 UTC
    Well, you can't (easily). Environment variables are only inherited to sub processes, not to the parent process.

    So you have to call setParam.sh first, and then run the perl script. If you want to do it from within perl, you can try something along this lines:

    unless ($ENV{INCLUDE}){ exec("setParam.sh; $^X $0 @ARGV"); }

    (update: added missing quote, shmem++)

Re: How do I set env. Variables from Perl with shell script
by Corion (Patriarch) on May 04, 2009 at 12:47 UTC
Re: How do I set env. Variables from Perl with shell script
by rovf (Priest) on May 04, 2009 at 12:38 UTC

    If the shell is bash, Env::Bash might help here. You would give 'setParam.sh' as parameter. Note, however, that you would not only get (exported) environment variables, but (non-exported) shell variables as well. I have never tried Env::Bash myself though, so I don't know how well it works.

    -- 
    Ronald Fischer <ynnor@mm.st>
Re: How do I set env. Variables from Perl with shell script
by JavaFan (Canon) on May 04, 2009 at 13:22 UTC
    It depends on your OS. Probably the easiest (as, requires the fewest lines of code changes) is to run it on VMS, as that OS allows child processes to update the environment variables of the parent.

    If you are running a Unix of a Windows flavour, you may have to result to trickery: for instance, by creating a wrapper (shell) program that sources your existing shell script, then executes your Perl program.

Re: How do I set env. Variables from Perl with shell script
by jmcada (Acolyte) on May 04, 2009 at 17:26 UTC
    I wrote/use Env::Sourced one day when my cpan search foo was weak and I couldn't find any other alternatives. It should do what you need it to do.
      Thanks All.
      Finally have written the following codes:
      my $HOME = $ENV{HOME}; my $shell = shift || (getpwuid($<))[8]; my $command = "${HOME}/.project_setup"; my @setup = `$shell -c \". $command ; env\"`; foreach (@setup) { if (/^(\S+)=(.*)$/) { my ($key, $value) = ($1, $2); #print ("$key => $value\n"); $ENV{$key} = $value; } } print "$ENV{INCLUDE}\n";
        I am a bit confused or may be dont understand, but how you are passing HOME variable here in perl script from that shell script.
        Noame, I'm a bit confused at this code. Would you please give an example using the PERL5LIB=/my1stdir/my2nddir/lib as the environmental variable in the code. Many thanks ...
Re: How do I set env. Variables from Perl with shell script
by jettero (Monsignor) on May 04, 2009 at 12:00 UTC

    You can't, without doing strange things like the answers below. The basic problem is that when you fork a shell and create variables, there isn't an interface to propagate them back upwards.

    -Paul

Re: How do I set env. Variables from Perl with shell script
by december (Pilgrim) on May 04, 2009 at 14:16 UTC

    You can't. If you execute the shell script from the perl script, it's a child process and the shell variables won't ever make it into perl's "upstream" environment.

    Perhaps you can manually parse the shell script and look for the INC(LUDE) variable, and then set the ENV from within perl manually based on the value found in the shell script... Depending on where exactly you want to with this, it might do the trick.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://761703]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2024-04-18 06:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found