Re: How to parse UNIX commands ...
- From: krahnj@xxxxxxxxx (John W. Krahn)
- Date: Fri, 27 Oct 2006 00:17:29 -0700
benbart@xxxxxxxxxxxx wrote:
Hi all,
Hello,
How do I parse or evaluate the output of UNIX commands?
For example, in UNIX scripts, I can run filesystem=`df -k | awk -F"" ' { print
$6 }'` to check for the mount points.
How do I achieve the same in Perl? That is, I tried:
#!/usr/bin/perl
$x=system 'df -k';
print "============================= \n";
print $x . "\n";
The usual way to do it:
#!/usr/bin/perl
use warnings;
use strict;
open my $PIPE, 'df -k |' or die "Cannot open pipe from 'df' $!";
print '=' x 29, "\n";
while ( <$PIPE> ) {
next if $. == 1; # skip first line -- header
my $mount_point = ( split )[ 5 ];
print "$mount_point\n";
}
close $PIPE or warn $! ? "Error closing 'df' pipe: $!"
: "Exit status $? from 'df'";
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
.
- References:
- How to parse UNIX commands ...
- From: benbart
- How to parse UNIX commands ...
- Prev by Date: Re: Help, using script to edit router config (entering different modes automatically)
- Next by Date: Re: Analize Java source file with perl?
- Previous by thread: Re: How to parse UNIX commands ...
- Next by thread: RE: How to parse UNIX commands ...
- Index(es):
Relevant Pages
|