2

I need some quick advice on perl script. I created a script that basically calls other perl scripts and many other shell scripts within those. The problem I'm facing, is simply trying to make this run on a universal level by setting one environment variable.

This is on linux RHEL/CentOS by the way...

so I add my variables to .bash_profile and it works without an issue if I MANUALLY source the file first, then run my perl script! This is, OK, but I would like the script to automate this part instead of needing the extra, manual sourcing step.

So my script looks like this... in short

#!/usr/bin/perl
use warnings;
use strict;

`/bin/bash ~/.bash_profile`;

blah blah blah more code etc;

When launching the main script (where this part of the code is) it works no problem. It's all the subsequent calls made to other scripts that are failing...as if it is not passing the variable on to the rest of the scripts.

Any ideas??

Thanks,

1
  • I don't know perl, but I guess it would be relevant to include the code actually calling the other scripts. Commented May 29, 2014 at 20:52

4 Answers 4

7
  • The easiest way would be to set the environment variables within perl with $ENV{"name"}=.... These variables then will be propagated automatically to any programs started from within the perl script, no matter if perl or shell scripts
  • alternatively you could open the .bash_profile, parse it within perl, extract the variables and then set them again within perl with $ENV. This is error prone because there might be several ways to declare the variables.
  • or you could spawn a shell, which reads the .bash_profile and calls env afterwards. Then you read and parse the output from this shell. This is similar to the previous proposal, but the output you have to parse is more clearly defined.
  • or you could use a shell script which sources .bash_profile and then spawns the perl script.
Sign up to request clarification or add additional context in comments.

6 Comments

The last options is the easiest one.
Ok Ill give this a shot... can the $ENV{"name"}= be set similar to scalars and arrays? I mean, can i do something like $ENV{"VARI"}=/bin/cat ~/.bash_profile | sed 's/VARI=//g';
Not at home where my script is or else id just try instead of asking lol... sorry for my ignorance
Well, I just created a small script to try it out and no go. Looks like I'll need to come up with an alternative...possibly adding the . ~/.bash_profile to the header of each script may be my only solution. Thanks for the help guys
Actually, the easiest way is to test for the necessary members of %ENV being set, and failing that die with a message for the user to configure their account correctly before running your script.
|
1

Environment variables are inherited by child processes from their parent, so you simply need to launch perl from a shell that sourced ~/.bash_profile.

bash login shells source that file automatically, so it's just a question of setting bash as your login shell. You could also use bash -l.

bash -lc 'exec script.pl args'

If you can't setup the correct environment before launching Perl, you can bootstrap a bash login shell.

if (@ARGV && $ARGV[0] eq 'setup') {
   shift(@ARGV);
} else {
   exec('bash', '-lc', 'exec "$@"', '-', $^X, $0, 'setup', @ARGV) or die $!;
}

In all three cases, the variable is accessed using

$ENV{VAR_NAME}

1 Comment

I will have to try this again... ultimately, I just ended up adding the . ~/.bash_profile to the 2nd line of all my scripts and it works as expected now. It's really a dummy proof right... .bash_profile is sourced on login, so this is more so to prevent a "novice" person from having to know how to source the profile, or log out then back in again. The idea is to be able to take the .tar package with my scripts and its subdirectories, and have them work without much intervention...simply adding the variable to .bash_profile and closing the editor
1

Backticks (and system, and open, etc.) spawns a separate process that inherits the environment from your Perl program, but can only affect its own environment, not propogate changes back to your Perl script.

Shell::GetEnv has a few tricks that will help you incorporate those changes to the child environment, but often times you will find it is easier to parse .bash_profile yourself.


The more recent Env::Modify module can handle this task, leveraging the tricks in Shell::GetEnv.

use Env::Modify 'source';
source("$ENV{HOME}/.bash_profile");

Comments

0

using $ENV we can get any value from .profile/.bash_profile Example: suppose any variable is store in your .profile/.bash_profile

export CONNECT="Connection"

Retrieve the same variable in your perl scripts from .profile/.bash_profile as:

my $DB_CONNECT = $ENV{CONNECT};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.