Get shell command return value in Perl program.

D

Dan

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!
 
I

it_says_BALLS_on_your_forehead

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().
 
J

John W. Krahn

Dan said:
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
 

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