Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

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:


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.:

#!/bin/sh blah=/tmp/test balh3=/xyx/zzz
#!/bin/sh source something...
#!/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...


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 should end with

    export blah balh3
    Then, in, 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!

      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 :

      ...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 # source printenv
        #!perl my $myenv = `` 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 |
    my perl scipt,, would print the following to STDOUT:


    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.

    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 ...