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

I have a .cgi running that needs to set a rather staggering large number of %ENV variables... and since there's a *.conf.sh sitting around with the exact list of exports, etc. that I need, I'd like to just use the shell script to set the variables.

I'm attempting to appease Laziness at this point, I realize, but maintaining a list of over a hundred of these buggers doesn't really excite me. At all.

Thanks in advance...
markguy

  • Comment on Retrieving evironment variables from a shell script

Replies are listed 'Best First'.
Re: Retrieving evironment variables from a shell script
by lhoward (Vicar) on Aug 02, 2000 at 00:29 UTC
    If all your .sh file does it set environmental vars in a static way (no conditional statments in the .sh, etc...) you could just read and parse the .sh file to set your %ENV vars.
    open F,"<conf.sh" or die $!; while(<F>){ chomp; if($_=~/([\w\_]+)\s*\=\s*(.+?)\s*$/){ $ENV{$1}=$2; } } close F;
    or something like that.....
Re: Retrieving evironment variables from a shell script
by ray (Initiate) on Aug 02, 2000 at 02:58 UTC
    Try the following, it doesn't make many assumptions about the conf file.
    my $rcfile = 'testrc'; # sets TESTEX to "test!" if(open(CHILD_TO_READ, ". $rcfile; env | sort |")) { while (<CHILD_TO_READ>) { my ($key, $val) = split '='; $ENV{$key} = $val; }; } print $ENV{TESTEX};
    Later,
    Ray.
      Actually, this script makes one unfriendly assumption: That none of the environment variables have values that include newlines.

      How about this, which is also shorter and simpler:

      my $rcfile = 'testrc'; # exports environment vars %CFG = split /\0/, `. $rcfile; $^X -e 'print join("\\0", %ENV)'`;

      (Special var $^X is the patch to the perl binary. And the double backslash is important.)

      If there's any possibility that the config file might do some printing to stdout, you have to get a little more paranoid. Otherwise, this should be fine.

          -- Chip Salzenberg, Free-Floating Agent of Chaos

RE: Retrieving evironment variables from a shell script
by steveAZ98 (Monk) on Aug 02, 2000 at 00:24 UTC
    You could just execute that script, if you can trust it's contents.
    `/path/to/script`;
    Note: those are back"ticks"

    or
    system('/path/to/script');
    Update:
    This won't work, see Fastolfe's post. Sorry.
      Note that this won't work.

      The script is executed in a child process. Changes to the environment are only accessible to that process. Exported environment variables are accessible to all children processes, but not necessarily the parent. The environment variable changes done by script.sh will be lost as soon as script.sh exits.

      The only way to get environment variables in this session is to parse them in as the other poster suggests. The alternative would be to use a shell script or something as your CGI (a wrapper, really), and have it source the conf.sh script first, followed by exec'ing your Perl CGI script.

        Thanks for the explanation... I wasn't sure how the script was handled.

        And for the record, I'm going with the parse technique suggested below... appreciate that as well!

        Take care folks...
        markguy

      I did indeed try both of those... oddly, neither worked. (btw, I believe you mean to point out that there are backticks, not backslashes in the first line... I knew what you meant ;)

      I think there's some sort of issue with the fact that when the shell script is invoked, it forks, so that by the time the perl script picks back up, the %ENV is back to being blank. But I could be full of it, as this isn't my area of expertise...

      Thanks anyway for the help... it's good to know that I wasn't way off when I tried that myself :)
      markguy