Using dot commands in a script

D

danfperl

I'm trying to load an environment file in a script and I think it may
be easiest to show it before explaining it. I've got 2 files,
/tmp/test.pl and /tmp/test.env. Here are the contents.

test.env:
#!/usr/bin/ksh
export BLAH=TEST

test.pl:
#!/usr/local/bin/perl
system('. /tmp/test.env');
print "\$BLAH is ${BLAH}" ;

When I run it I get:
sh: BLAH=TEST: is not an identifier
$BLAH is

How can I get Perl to load the file properly? It does so from the
command line but throws the error when I run the script. For reasons I
can't get into I can't change the env file - the perl script needs to
be modified. Any help is greatly appreciated!!

Dan
 
M

Martin Kissner

I'm trying to load an environment file in a script and I think it may
be easiest to show it before explaining it. I've got 2 files,
/tmp/test.pl and /tmp/test.env. Here are the contents.

test.env:
#!/usr/bin/ksh
export BLAH=TEST

test.pl:
#!/usr/local/bin/perl
use strict;
use warnings
system('. /tmp/test.env');

Why not simply say:
$ENV{BLAH}="TEST";
to set the environmet variable
or even
$BLAH="TEST";
to use a perl variable?
print "\$BLAH is ${BLAH}" ;
Here you probably want:
print "Shell-\$BLAH is $ENV{BLAH}\n";
if you really want the exported shell variable.

${BLAH} is the same as $BLAH, which has not been assigned yet in your
script.
When I run it I get:
sh: BLAH=TEST: is not an identifier

This comes from the shell; I have not worked with the ksh, but it looks
like a syntax error in the variable assignment.

This is, because $BLAH is empty.
If you had used strict and warnings, perl had told you.

HTH
Best regards
Martin
 
D

Dan

Thanks for the quick reply Martin.
Why not simply say...to set the environmet variable or even...to use
a perl variable?

It's not just one variable - that was an example. The actual
environment file I'm working with has dozens of lines. We have lots of
scripts running here and decided to have a single file with all
settings that anyone could reference. The more we set environment
variables outside it the tougher it is to manage, so there's basically
a hard line on that one: Source from the environment file or not at
all.

I know the error is shell related and I know why it's throwing it. For
some reason the dot command in the script defaults to sh instead of ksh
or bash and the environment file was designed for ksh/bash. When it
runs via sh it throws the error. (It uses "export BLAH=TEST" syntax
which isn't supported in sh.)

Dan
 
M

Martin Kissner

I'm trying to load an environment file in a script and I think it may
be easiest to show it before explaining it. I've got 2 files,
/tmp/test.pl and /tmp/test.env. Here are the contents.

test.env:
#!/usr/bin/ksh
export BLAH=TEST

test.pl:
#!/usr/local/bin/perl
system('. /tmp/test.env');
print "\$BLAH is ${BLAH}" ;

When I run it I get:
sh: BLAH=TEST: is not an identifier
$BLAH is

How can I get Perl to load the file properly? It does so from the
command line but throws the error when I run the script. For reasons I
can't get into I can't change the env file - the perl script needs to
be modified. Any help is greatly appreciated!!

Well, in that case I would try
system('/usr/bin/ksh /tmp/test.env')
I suppose even
system('/tmp/test.env')
would work if /tmp/test.env is executable, since it has a proper shebang
line.

But I guess I'd prefere a file wich is formatet like this
----- variables.txt ----
name1:value1
name2:value2
name3:value3
----
and then use a hash like this in the scripts
--- snip ----
my %hash;
open FH,"<","variables" or die "error\n";
while (<FH>) {
/(.*?):(.*)/;
$hash{$1}=$2;
}
print $hash{'name1'},"\n";
--- snap ---

The last line prints "value1".
I do not really see a need to use the 'system' function and shell
variables here.

HTH
Martin
 
T

Tony Curtis

On 9 Mar 2005 12:10:55 -0800,
I'm trying to load an environment file in a script and I
think it may be easiest to show it before explaining it.
I've got 2 files, /tmp/test.pl and /tmp/test.env. Here are
the contents.
test.env:
#!/usr/bin/ksh
export BLAH=TEST
test.pl:
#!/usr/local/bin/perl

warnings & strict?
system('. /tmp/test.env');

system() doesn't use ksh (hence the error message), and those
settings will only exist within that transient child process.
print "\$BLAH is ${BLAH}" ;

Now back to the parent, child's environment is gone! Also the
child process would not have been able to set a variable
inside the perl program.
When I run it I get: sh: BLAH=TEST: is not an identifier
$BLAH is

You could write some perl to parse the export settings in the
ksh script, and then set $ENV{} within the perl.

Or wrap the perl up inside a script that does the source
first.

Is this a FAQ? I couldn't find anything obvious with -q.

hth
t
 
R

robin

try running in linux, and if that doesn't work, run it in a command
prompt with the perl -M compiler, not that I know how to do that now;
if that still doesn't work, try the exec command or use backtix.
-robin
 
S

Sherm Pendley

robin said:
try running in linux, and if that doesn't work, run it in a command
prompt with the perl -M compiler, not that I know how to do that now;
if that still doesn't work, try the exec command or use backtix.

Good God, where to start...

He *is* running it in *nix, where do you think he's got ksh installed, and
Perl in /usr/local/bin?

The -M switch tells perl to load a module. It's got nothing at all to do
with the Perl compiler.

And last (but certainly not least) his use of system() vs. exec() or
backticks has *nothing* to do with the problem.

sherm--
 
J

Josef Moellers

I'm trying to load an environment file in a script and I think it may
be easiest to show it before explaining it. I've got 2 files,
/tmp/test.pl and /tmp/test.env. Here are the contents.

test.env:
#!/usr/bin/ksh
export BLAH=TEST

test.pl:
#!/usr/local/bin/perl
system('. /tmp/test.env');
print "\$BLAH is ${BLAH}" ;

When I run it I get:
sh: BLAH=TEST: is not an identifier
$BLAH is

How can I get Perl to load the file properly? It does so from the
command line but throws the error when I run the script. For reasons I
can't get into I can't change the env file - the perl script needs to
be modified. Any help is greatly appreciated!!

Using system() will spawn a new shell which will modify its own
environment and pass any modifications on to its children. Nothing is
exported upwards or sidewarts the process hierarchy. It's pretty much
like trying to "cd" in a shell script and find that you stayed where you
are in your current shell.

There is, as usual, more than one way to do it:

1. you could do the definition in a perl-file and "require" this file:
test.env:
our $BLAH = 'TEST';

test.pl:
#! /usr/bin/perl

use warnings;
use strict;
our $BLAH;

require "test.env";

print "BLAH is $BLAH\n";

2. you could read and parse the shell script:
test.env:
#!/usr/bin/ksh
export BLAH=TEST

test.pl:
#! /usr/bin/perl

use warnings;
use strict;

if (open ENV, '<', 'path/to/test.env') {
while (<ENV>) {
next if /^\s*#/;
chomp;
if (/^\s*(export)?\s+([^=]+)=(.*)/) {
my $var = $2;
my $value = $3;
$value =~ s/^"(.*)"$/$1/;
$ENV{$var} = $value;
}
}
close ENV;
}
print $ENV{BLAH}, "\n";

(Note that there are several traps lurking, e.g. the following would be
accepted by this script but not by ksh:
export BLAH=This is a test
)

HTH,

Josef
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,008
Latest member
HaroldDark

Latest Threads

Top