Passing a variable from Bourne Shell to Perl

Discussion in 'Perl Misc' started by tpk142000@yahoo.com, Aug 15, 2005.

  1. Guest

    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.
    , Aug 15, 2005
    #1
    1. Advertising

  2. Paul Lalli Guest

    wrote:
    > 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
    Paul Lalli, Aug 15, 2005
    #2
    1. Advertising

  3. kartik Guest

    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 Lalli wrote:
    > wrote:
    > > 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
    kartik, Aug 16, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. biner
    Replies:
    2
    Views:
    340
    Fredrik Lundh
    Dec 21, 2004
  2. Dan Stromberg

    bourne shell parser in python

    Dan Stromberg, Apr 8, 2005, in forum: Python
    Replies:
    0
    Views:
    358
    Dan Stromberg
    Apr 8, 2005
  3. Christian Heimes
    Replies:
    0
    Views:
    584
    Christian Heimes
    Feb 27, 2008
  4. Replies:
    3
    Views:
    135
    Darren Dunham
    Jan 26, 2005
  5. Ilya Zakharevich

    Detecting Bourne (or csh) shell from Perl

    Ilya Zakharevich, Mar 3, 2010, in forum: Perl Misc
    Replies:
    8
    Views:
    183
    Ilya Zakharevich
    Mar 10, 2010
Loading...

Share This Page