Re: How to parse UNIX commands ...



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
.



Relevant Pages

  • RFC: executable archive
    ... A problem with perl, that Larry Wall should have though of right? ... The problem is compounded that all decent scripting languages allow ... hard on the developers of the scripting engine to make single exec. ... use the loopback mount device to mount the ar under ...
    (Linux-Kernel)
  • Re: system(@args) query, result not as expected.
    ... The error is *nix, not perl, but, please bear with me and read on. ... mount: unknown filesystem type 'password=drowssap"' ... Any ideas why the mount command was thinking that 'password' was the ... When you made it all one string, ...
    (comp.lang.perl.misc)
  • Re: Capture stderr stdout using system call with commas?
    ... > I'm trying to run some mount and rsync commands from inside a perl ... > another option of mount and bombs out. ... I have several perl scripts that run as cron jobs and I want to capture ... The shell script which executes the perl program clean-archive is as ...
    (comp.lang.perl.misc)
  • Re: Regex confusion
    ... By the way, looking at some examples of regex, I see constructs like ... 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 ...
    (comp.lang.perl.misc)
  • Re: regex question
    ... Tom Allison wrote: ... 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 ...
    (perl.beginners)