Get shell command return value in Perl program.

Discussion in 'Perl Misc' started by Dan, Aug 14, 2007.

  1. Dan

    Dan Guest

    Hi all, I have a Perl script which includes a shell command, I want to
    get the return result from this shell command, but when I check the $?
    value, it is not correct, can anyone tell me how I can get the return
    value?

    my perl code:

    opendir(DIR, "/log") || die ("Can not open log directory");
    foreach $direntry (readdir(DIR))
    {
    if ($direntry =~ /2007\d{4}/)
    {
    $ja_request = system("grep -i 'INDEX' $BASE/$direntry/
    FILE.log | wc -l");
    }
    }

    Basically, I want to search the "INDEX" string, and count the number
    of occurrence in file.LOG file. But the
    $ra_request always is 0.

    Can anyone help about this, thanks a lot!
     
    Dan, Aug 14, 2007
    #1
    1. Advertising

  2. On Aug 14, 5:58 pm, Dan <> wrote:
    > Hi all, I have a Perl script which includes a shell command, I want to
    > get the return result from this shell command, but when I check the $?
    > value, it is not correct, can anyone tell me how I can get the return
    > value?
    >
    > my perl code:
    >
    > opendir(DIR, "/log") || die ("Can not open log directory");
    > foreach $direntry (readdir(DIR))
    > {
    > if ($direntry =~ /2007\d{4}/)
    > {
    > $ja_request = system("grep -i 'INDEX' $BASE/$direntry/
    > FILE.log | wc -l");
    > }
    > }
    >
    > Basically, I want to search the "INDEX" string, and count the number
    > of occurrence in file.LOG file. But the
    > $ra_request always is 0.
    >
    > Can anyone help about this, thanks a lot!


    Why use Perl for this? Why not just loop through it via your shell?
    Anyway, if you want the output of a command that you 'shelled out',
    just use the backtick operator instead of system().
     
    it_says_BALLS_on_your_forehead, Aug 14, 2007
    #2
    1. Advertising

  3. Dan wrote:
    > Hi all, I have a Perl script which includes a shell command, I want to
    > get the return result from this shell command, but when I check the $?
    > value, it is not correct, can anyone tell me how I can get the return
    > value?
    >
    > my perl code:
    >
    > opendir(DIR, "/log") || die ("Can not open log directory");
    > foreach $direntry (readdir(DIR))
    > {
    > if ($direntry =~ /2007\d{4}/)
    > {
    > $ja_request = system("grep -i 'INDEX' $BASE/$direntry/
    > FILE.log | wc -l");
    > }
    > }
    >
    > Basically, I want to search the "INDEX" string, and count the number
    > of occurrence in file.LOG file. But the
    > $ra_request always is 0.
    >
    > Can anyone help about this, thanks a lot!


    my $ja_request;
    { local @ARGV = glob '/log/*2007[0-9][0-9][0-9][0-9]*/FILE.log';
    while ( <> ) {
    $ja_request++ while /\bINDEX\b/g;
    }
    }



    John
    --
    Perl isn't a toolbox, but a small machine shop where you
    can special-order certain sorts of tools at low cost and
    in short order. -- Larry Wall
     
    John W. Krahn, Aug 15, 2007
    #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. Replies:
    2
    Views:
    5,746
    Jonathan Bromley
    Feb 18, 2005
  2. Yann.K
    Replies:
    4
    Views:
    531
    klappnase
    Sep 9, 2004
  3. Replies:
    4
    Views:
    1,723
    Terry Hancock
    Apr 23, 2005
  4. Donald Duck

    Best command for running shell command

    Donald Duck, Jul 11, 2006, in forum: Python
    Replies:
    3
    Views:
    417
    iapain
    Jul 11, 2006
  5. Greenhorn
    Replies:
    15
    Views:
    839
    Keith Thompson
    Mar 6, 2005
Loading...

Share This Page