conditioning system shell output from within Perl - Noob

J

jason

When I execute the folloing at the HPUX Posix shell

diffmsg=`scripts/cdiff.sh`
print $diffmsg

I get the echo output from that script.

However, inside Perl, apparently once the shell system call closes,
that environment variable is gone as $diffmsg never has anything in
it.

system "diffmsg=`scripts/cdiff.sh` ";
$diffmsg = ($ENV{'diffmsg'});

Am I correct in my findings?

Any easy way to be able to condition the output from this shell script
in perl? I'm hoping without having to open, read and close a file.

Thanks.
 
K

Keith Keller

When I execute the folloing at the HPUX Posix shell

diffmsg=`scripts/cdiff.sh`
print $diffmsg

I get the echo output from that script.

However, inside Perl, apparently once the shell system call closes, ^^^^^^^^^^^
that environment variable is gone as $diffmsg never has anything in
it.

That's because once the system call is done, the shell where the
variable was set is also gone (if one was called).
Any easy way to be able to condition the output from this shell script
in perl? I'm hoping without having to open, read and close a file.

perldoc -q output.*system

--keith
 
T

Tad McClellan

However, inside Perl, apparently once the shell system call closes,
that environment variable is gone as $diffmsg never has anything in
it.


Yes, that is standard Unix process model stuff, the same with any
programming language.

A child cannot affect it's parent's environment.

system "diffmsg=`scripts/cdiff.sh` ";
Any easy way to be able to condition the output from this shell script
in perl?

Yes.


I'm hoping without having to open, read and close a file.


Well you *are* going to have to open and read the documentation
for the system() function.

I gotta wonder how it is that you haven't done that already...

It says right in there how to capture the output from external programs.
 
S

Sherm Pendley

Any easy way to be able to condition the output from this shell script
in perl? I'm hoping without having to open, read and close a file.

As far as the environment variable being visible to the parent Perl app,
that's a FAQ. Have a look at "perldoc -q environment".

But you don't need an environment variable at all - just capture the
output of scripts/cdiff.sh directly. Backticks work in Perl the same way
they do in your shell, so in Perl do:

my $diffmsg = `scripts/cdiff.sh`;

sherm--
 
T

Tintin

When I execute the folloing at the HPUX Posix shell

diffmsg=`scripts/cdiff.sh`
print $diffmsg

I get the echo output from that script.

However, inside Perl, apparently once the shell system call closes,
that environment variable is gone as $diffmsg never has anything in
it.

system "diffmsg=`scripts/cdiff.sh` ";
$diffmsg = ($ENV{'diffmsg'});

Am I correct in my findings?

Any easy way to be able to condition the output from this shell script
in perl? I'm hoping without having to open, read and close a file.

The following two shell scripts should answer your question.

#!/bin/sh
echo "script1"
../script2
echo "$diffmsg is not set because my child process has completed"

#!/bin/sh
echo "script2"
diffmsg='foo'
export diffmsg
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top