Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

get environment variables from a bourne file

by BradV (Sexton)
on May 12, 2011 at 13:59 UTC ( [id://904412]=perlquestion: print w/replies, xml ) Need Help??

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

I have a bourne shell script that sets a bunch of environment variables. From another bourne shell script I would just call as: . /usr/local/env.sh

I've written a perl script that I need to get those same environment variables set in. I tried doing:

my %BB_ENV = `/bin/sh . /usr/local/env.sh`; %ENV = { %ENV, %BB_ENV };

Any ideas how I could achieve this?

Thanks!

Hopefully, I am updating this correctly. :)

I couldn't get any of the suggestions to work. The file has comments, "export" lines, etc. Not a simple "Var=value" type of setup. So, I wound up doing this:

#!/bin/sh if [ -f /usr/local/env.sh ]; then . /usr/local/env.sh fi exec perl -wx "$0" "$@" #!/usr/bin/perl -w ....

That is working. Thanks for all of the suggestions!

Replies are listed 'Best First'.
Re: get environment variables from a bourne file
by kennethk (Abbot) on May 12, 2011 at 14:19 UTC
    Some commentary on your posted code:
    • my %BB_ENV = `/bin/sh . /usr/local/env.sh`;

      Backticks (`STRING`) return a simple scalar. Assigning this to a hash gives you a single, very long key associated with an undefined value, and will throw a warning when you use warnings (You are using warnings, right?). In order to obtain a set of key-value pair, you need to process that string. Assuming an output like printenv, you might use a pair of splits, like

      my %BB_ENV; for my $line (split /\n/, `/bin/sh . /usr/local/env.sh`) { my ($key,$value) = split /=/, $line, 2; $BB_ENV{$key} = $value; }
    • %ENV = { %ENV, %BB_ENV };

      A pair of curly brackets gives you an anonymous hash reference, not a hash. A hash ref is a scalar - see perlreftut. You probably mean just a simple list assignment, which would use parentheses:

      %ENV = ( %ENV, %BB_ENV );

      though I would instead manually copy in values, key by key:

      $ENV{$_} = $BB_ENV{$_} for keys %BB_ENV;

    In any case, you should probably follow Anonymous Monk's links above for some other approaches to solving this issue.

      Sometimes, variables might get set as
      x=$(some-command)
      or even, horrors:
      eval $x=6
      You can use set to list all the variables and their values after running the script.

      Thanks to all. I'll take a look. Right, I'm trying to add to the default %ENV hash. I have a perl module that I wrote that gets a couple of variables out of it. It works fine under Solaris 10, and RHEL 5. However, RHEL 6 seems to treat the environment slightly differently. The variables that I get from env.sh in the other OS's work fine. However not showing up in RHEL 6. So, the reason for this little bit a kludge. :)

Re: get environment variables from a bourne file
by toolic (Bishop) on May 12, 2011 at 14:20 UTC
    Unrelated to your main problem...
    %ENV = { %ENV, %BB_ENV };
    If you use strict and warnings, it will alert you that there is something wrong with this way of merging hashes:
    use warnings; use strict; use Data::Dumper; my %BB_ENV = (zzz => 555); %ENV = { %ENV, %BB_ENV }; print Dumper(\%ENV); __END__ Reference found where even-sized list expected at $VAR1 = { 'HASH(0x6f45f0)' => undef };
    diagnostics shows that you should use parens () instead of curlies {}.
Re: get environment variables from a bourne file
by Anonymous Monk on May 12, 2011 at 14:03 UTC
Re: get environment variables from a bourne file
by anonymized user 468275 (Curate) on May 12, 2011 at 14:26 UTC
    something like:
    %ENV = ( %ENV, map { /^(\w+)\=(.*)$/; } split( /\n/, `. /usr/local/env.sh; env` ) );

    One world, one people

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2024-04-24 08:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found