Manish Tiwari wrote:
> Hi
> 
> i was trying to do something like
> 
> @data = `ls -ltr ` ;
> 
> < do some operation check whether ls command actually worked>
> 
> now i want to pass the contents of "data" thru unix commands like egrep 
> and sort
> 
> 
> so in all operation will be like
> 
> ls -ltr | grep -v total | sort +8
> 
> but making sure that ls command actually worked

use strict;

my @data;

@data = `ls -ltr `;

print "\$? = '$?'\n";
print "data[0] = '$data[0]'\n" if @data;

#try one of these two tests (with and/or without the $? part)

if (not $? and @data and $data[0] !~ /^ls:/i) {
        print "1 on to piping\n";
}

if ($? or (@data and $data[0] !~ /^total/i)) {
        print "Error\n";
}
exit;

__END__

$? may still return 0 (depending on app) and output the error message to stderr.
$? >> 8 would be the real exit status of the `` command.

If you redirect stderr to stdout, you may be able to trap the error
message which will hopefully be 'ls: <text>'.  You may have to use a
pipe and redirect 2>&1 to get stderr redirected.

-- 
   ,-/-  __      _  _         $Bill Luebkert   ICQ=162126130
  (_/   /  )    // //       DBE Collectibles   Mailto:[EMAIL PROTECTED]
   / ) /--<  o // //      http://dbecoll.tripod.com/ (Free site for Perl)
-/-' /___/_<_</_</_     Castle of Medieval Myth & Magic http://www.todbe.com/

_______________________________________________
Perl-Unix-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to