Get shell command return value in Perl program.
Get shell command return value in Perl program.
am 14.08.2007 23:58:43 von 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!
Re: Get shell command return value in Perl program.
am 15.08.2007 00:14:33 von simon.chao
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().
Re: Get shell command return value in Perl program.
am 15.08.2007 00:25:35 von Bob Walton
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.
....
wc -l does not return the number of lines, it prints it to standard
output. You probably want to use qx() (the "backtick" operator) in
order to obtain that standard output. Maybe something like:
$ja_request=`grep -i 'INDEX' $BASE/$direntry/FILE.log|wc -l`;
--
Bob Walton
Email: http://bwalton.com/cgi-bin/emailbob.pl
Re: Get shell command return value in Perl program.
am 15.08.2007 05:05:16 von Dummy
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