Passing a variable from Bourne Shell to Perl

T

tpk142000

I am calling a shell script from perl, where the shell creates a
filename and inserts data in it. Now, I'll have to get the filename
variable from shell to perl when shell completes its execution and
returns back to perl. I want the Shell variable(filename) to print its
contents to the user.

I am having problems fetching the filename variable from shell.

Here is the snippet of my code in Perl:

system();
my $cmd= '/export/home/kanser/site/gurapay/gurapay_feed.shl';
system ($cmd) == 0 or die "Cannot run $cmd:$?\n";
my $variable_from_shell=$ARGV[0];
print "Variable form Shell is: $variable_from_shell\n";

I know the last two lines are not right. Can you please suggest me how
to proceed with this.

Thanks.
 
P

Paul Lalli

I am calling a shell script from perl, where the shell creates a
filename and inserts data in it. Now, I'll have to get the filename
variable from shell to perl when shell completes its execution and
returns back to perl. I want the Shell variable(filename) to print its
contents to the user.

Exactly how does this shell program "return" the filename?
I am having problems fetching the filename variable from shell.

Here is the snippet of my code in Perl:

system();

What do you believe this is doing? This looks like an attempt to
execute a null external program. In fact, perldoc -f system doesn't
even say what would happen if system is called with no arguments. I'm
somewhat surprised this isn't a compilation error.
my $cmd= '/export/home/kanser/site/gurapay/gurapay_feed.shl';
system ($cmd) == 0 or die "Cannot run $cmd:$?\n";

This command is now done. The only trace that it ever ran is that its
numeric return value is stored in $?.
my $variable_from_shell=$ARGV[0];

$ARGV[0] is the first argument passed to the *Perl* script. It has
absolutely nothing to do with the external command you just ran.
print "Variable form Shell is: $variable_from_shell\n";

I know the last two lines are not right. Can you please suggest me how
to proceed with this.

There are a couple possibilities, of varying levels of difficulty. You
might want to have your shell script save the value you're looking for
in a file, and then have Perl read that file once the command is
executed. You could have the shell script access a database and store
the value there. Or you could have the shell script print the value
you're looking for to STDOUT, and read the STDOUT of the shell script
rather than printing it to the screen. (That is, use backticks rather
than system)

your perl script (file.pl):
#!/usr/bin/perl
use strict;
use warnings;

my $cmd = 'shell.sh';
chomp (my $filename = `$cmd`);
print "'$filename' was returned from the shell\n";
__END__

your shell script (shell.sh)
#!/usr/bin/ksh
echo "foobar.txt";

$ file.pl
'foobar.txt' was returned from the shell
$


for more information:
perldoc perlipc - "InterProcess communication"
perldco perlop - search for "qx"

Paul Lalli
 
K

kartik

Paul and Stanislaw Klekot, thanks for your replies. Now, I understand
better that child process cannot pass a variable to the parent. I am
going to write the value of the variable to a file and make the perl
pick from it.


Paul said:
I am calling a shell script from perl, where the shell creates a
filename and inserts data in it. Now, I'll have to get the filename
variable from shell to perl when shell completes its execution and
returns back to perl. I want the Shell variable(filename) to print its
contents to the user.

Exactly how does this shell program "return" the filename?
I am having problems fetching the filename variable from shell.

Here is the snippet of my code in Perl:

system();

What do you believe this is doing? This looks like an attempt to
execute a null external program. In fact, perldoc -f system doesn't
even say what would happen if system is called with no arguments. I'm
somewhat surprised this isn't a compilation error.
my $cmd= '/export/home/kanser/site/gurapay/gurapay_feed.shl';
system ($cmd) == 0 or die "Cannot run $cmd:$?\n";

This command is now done. The only trace that it ever ran is that its
numeric return value is stored in $?.
my $variable_from_shell=$ARGV[0];

$ARGV[0] is the first argument passed to the *Perl* script. It has
absolutely nothing to do with the external command you just ran.
print "Variable form Shell is: $variable_from_shell\n";

I know the last two lines are not right. Can you please suggest me how
to proceed with this.

There are a couple possibilities, of varying levels of difficulty. You
might want to have your shell script save the value you're looking for
in a file, and then have Perl read that file once the command is
executed. You could have the shell script access a database and store
the value there. Or you could have the shell script print the value
you're looking for to STDOUT, and read the STDOUT of the shell script
rather than printing it to the screen. (That is, use backticks rather
than system)

your perl script (file.pl):
#!/usr/bin/perl
use strict;
use warnings;

my $cmd = 'shell.sh';
chomp (my $filename = `$cmd`);
print "'$filename' was returned from the shell\n";
__END__

your shell script (shell.sh)
#!/usr/bin/ksh
echo "foobar.txt";

$ file.pl
'foobar.txt' was returned from the shell
$


for more information:
perldoc perlipc - "InterProcess communication"
perldco perlop - search for "qx"

Paul Lalli
 

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,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top