LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 06-19-2006, 04:22 PM   #1
rose_bud4201
Member
 
Registered: Aug 2002
Location: St Louis, MO
Distribution: Xubuntu, RHEL, Solaris 10
Posts: 927

Rep: Reputation: 30
Perl with bash environment vars


I've got a perl script which needs to be aware of bash environment variables. I can use Env, but the problem is not all of the variables exist for the user that the script will be running as.

In bash, we accomplish this by having a shell script called env.sh which does a whole bunch of 'export VARIABLE=VALUE' calls.

Our shell scripts, then, do:
Code:
source /path/to/env.sh
db2 "connect to $db user $user using $blah"
What I need to do is that same thing, only in perl. `source /path/to/env.sh` gives me a "no such command as source" error message, and `. /path/to/env.sh` followed by "my $dbname = $VARIABLE" (where $VARIABLE is one of those exported in env.sh) doesn't error, but doesn't seem to do anything either.

I still get
Code:
$ ./testing.pl
Global symbol "$VARIABLE" requires explicit package name at ./testing.pl line 33.

Execution of ./testing.pl aborted due to compilation errors.
Does anyone know how to work around this? I'm really not supposed to simply parse the env file, since its format has been known to change at will.

Thanks!
 
Old 06-19-2006, 05:17 PM   #2
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33
I guess you'll have to set those environment variables in ~/.profile or in /etc/profile, or instead of invoking that Perl script directly, you can use a shell script that sets the variables and then proceeds with the Perl script.
 
Old 06-20-2006, 12:23 PM   #3
puffinman
Member
 
Registered: Jan 2005
Location: Atlanta, GA
Distribution: Gentoo, Slackware
Posts: 217

Rep: Reputation: 30
You can easily parse the file yourself. Here's one way to do it.

The input file:
Code:
export STUFF=things
export  LOOKIT=here
export SNAZZY=eh
The perl script:
Code:
#!/usr/bin/perl

my $varfile = "vartest";
my %vars;

open(FH,"<$varfile") or die "Couldn't open $varfile: $!\n";
while (<FH>) {
  chomp;
  if (m/export\s+(\w+)=(.+)$/) {
    $vars{$1}=$2;
  }
} 
close FH;

use Data::Dumper;
print Dumper %vars;
The output:
Code:
$VAR1 = 'STUFF';
$VAR2 = 'things';
$VAR3 = 'SNAZZY';
$VAR4 = 'eh';
$VAR5 = 'LOOKIT';
$VAR6 = 'here';

Last edited by puffinman; 06-20-2006 at 01:36 PM.
 
Old 06-21-2006, 10:05 AM   #4
rose_bud4201
Member
 
Registered: Aug 2002
Location: St Louis, MO
Distribution: Xubuntu, RHEL, Solaris 10
Posts: 927

Original Poster
Rep: Reputation: 30
Hmm...I may go the shell script route, rather than the parsing route. It's for work, and there are just things which I am and am not supposed to do

Thanks to both of you!
 
Old 06-21-2006, 10:30 AM   #5
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33
You can use the following construct, to make sure the Perl script won't be invoked independently:
Code:
#! /bin/bash

export NEW_VAR="nonsense"

exec perl <<EOF

# Here starts your Perl script
print join(",",sort( keys(%ENV)));

EOF
 
Old 06-21-2006, 12:03 PM   #6
rose_bud4201
Member
 
Registered: Aug 2002
Location: St Louis, MO
Distribution: Xubuntu, RHEL, Solaris 10
Posts: 927

Original Poster
Rep: Reputation: 30
Hmmm..that's a neat trick! I never would've thought of it, thank you

I'll check back and let you know how it works.
 
Old 06-21-2006, 12:19 PM   #7
rose_bud4201
Member
 
Registered: Aug 2002
Location: St Louis, MO
Distribution: Xubuntu, RHEL, Solaris 10
Posts: 927

Original Poster
Rep: Reputation: 30
Hurrah, success! I used a hybrid method:
Code:
#!/bin/bash
# Set some environment variables that testing.pl needs,
# and then call the script.

source /usr/local/bin/env.sh

echo "\$1 is: $1\n"
echo "\$2 is: $2\n"
echo "\$3 is: $3\n"
echo "\$4 is: $4\n"

/home/me/work/testing.pl $1 $2 $3 $4
It works, and outputs the correct variable values from inside the perl script.
Thanks so much!
 
  


Reply

Tags
bash, perl


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
SUSE Environment Vars apocolpse Suse/Novell 6 11-18-2010 03:04 PM
N00b: Simple Question about Bash Func and Vars: TylerD75 Programming 6 04-03-2005 06:03 AM
bash env vars alias's & .bashrc micxz Linux - General 8 10-08-2003 02:09 PM
how to set environment vars at GNOME terminal startup ? janisj Linux - General 8 07-30-2003 06:35 PM
Environment vars AMDPwred Linux - General 2 02-28-2003 07:11 AM


All times are GMT -5. The time now is 08:49 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration