Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Importing environment variables from shell script file to Perl

by bugsbunny (Scribe)
on Oct 23, 2003 at 11:59 UTC ( [id://301546]=perlquestion: print w/replies, xml ) Need Help??

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

hi,

I have some shell scripts and some perl scripts.. I store some paths and other variables as shell script.. Now I want to be able to import these vars into my perl-scripts.. ex.:

myenv.sh

#!/bin/sh blah=/tmp/test balh3=/xyx/zzz
myshell-script.sh
#!/bin/sh source myshell-script.sh ...do something...
myperl-script.pl
#!/usr/bin/perl ..what to do here...
I need some simpler&shorter solution, not f.e.:
open FILE ..; undef $/; $x =<FILE>; %hash = split /=/, (split /\n/, $x); close FILE ..;
Also what i should do if i decice to put some code..:"), probably not but in any case...

thanx

update (broquaint): added <code> tags and fixed formatting

Title edit by tye

Replies are listed 'Best First'.
Re: enviorment
by RMGir (Prior) on Oct 23, 2003 at 12:04 UTC
    I hope I understood the question correctly, but here goes:

    I'm not sure about "real" sh implementations, but bash acting as /bin/sh requires that you export your environment variables in order for subprocesses to see them.

    So your myenv.sh should end with

    export blah balh3
    Then, in myperl-script.pl, you can access those values as $ENV{blah} and $ENV{balh3}. See perldoc perlvar for more information about the %ENV "magical" hash.

    I hope this helps!


    Mike
      yep i was tring first this... but as i read more tha bash docs.. export-ed vars are seen only in child processes but my perl script is the parent what I mean :

      #!/usr/bin/perl
      qx{myenv.sh}
      ...more code...


      see, the env is not seen..why I need shorter solution, 'cause this will be added to many scripts and I dont want to repeat alot of similar code ..
        If the shell script that sets the variables is simple, then parsing that file is the best way to go. Substituting variable makes things a little harder but isn't too hard to accomplish in Perl. If it is more complicated than this, then it is best to let bash handle the shell scripts. Personally, I would have the config file be simple key=value format. It could be read by bash or perl, but would not be a proper shell script (no #!, no commands).

        You can use bash to run the shell script, print the environment with printenv, and parse the results with Perl

        #!bash # myenvprint.sh source myenv.sh printenv
        #!perl my $myenv = `myenvprint.sh` my %env = parse_env($myenv);
Re: enviorment
by Art_XIV (Hermit) on Oct 23, 2003 at 12:58 UTC

    The simplest solution of all would probably be to roll the functionality of your shell scripts into you perl code.

    If you can't do the above, then it may be that you're not familiar with (or haven't considered) piping, which might be a good choice for the other-than-environment-variables that need exported to your perl script.

    If you are familiar with piping, please ignore the rest of this response. It's real newbie stuff.

    There is a short tutorial on piping here.

    As an example, lets say that your shell script outputs the lines:

    fee fi fo fum

    A perl script that is expecting piped input would look something like:

    #!/usr/bin/perl -w use strict; while (<STDIN>) { print uc($_); }

    If I then 'piped' the output of my shell script into my perl script with a command like
    giant.sh | caps.pl
    my perl scipt, caps.pl, would print the following to STDOUT:

    FEE FI FO FUM

    This is a very trivial example. It would be much better to have your shell scripts print identifiers for each line so that there is no question about what line belongs to what other-than-environment variable.

Re: Importing environment variables from shell script file to Perl
by coreolyn (Parson) on Oct 23, 2003 at 18:19 UTC
Re: Importing environment variables from shell script file to Perl
by tbone1 (Monsignor) on Oct 23, 2003 at 15:28 UTC

    Another idea: in the shell script, do

    export blah=/tmp/test
    to make these environment variables. Then your Perl script can use the %ENV hash to read these in. Thus, $ENV{blah} will have the value "/tmp/test"

    Of course, this is assuming that's possible in this project, specs being specs and managers being managers.

    --
    tbone1
    Ain't enough 'O's in 'stoopid' to describe that guy.
    - Dave "the King" Wilson

      Ummm, no. He said specifically that the perl script is the parent of the shell script, not the other way round.

Re: enviorment
by bugsbunny (Scribe) on Oct 23, 2003 at 12:07 UTC
    oops, soorry i mean ...
    source myenv.sh

Log In?
Username:
Password:

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

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

    No recent polls found