Re: How to parse UNIX commands ...
- From: arnaldo@xxxxxxxxxxx (Arnaldo Guzman)
- Date: Fri, 27 Oct 2006 02:20:06 -0400
On Fri, 2006-10-27 at 17:30 +1300, benbart@xxxxxxxxxxxx wrote:
Hi all,
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";
And I am hoping that I can same the results of the df -k output into some kind
of variable that I can then parse and arrange into some format to check for
what logical volumes is mounted into what filesystems etc.
But x gives I believe the result of whether running the system command is
successful or not.
Maybe I can just simply run system 'df -k', re-direct that into some output file
and then parse it using OPEN-CLOSE filehandles. Is there any easier way of doing
this?
I am not just wanting to do this for the df -k command but also for executing ls
-l | wc -l maybe and some other UNIX commands.
Any advise will be very much appreciated. Thanks ....
You are correct, "$x = system("...");" will just store the result of
whether or not system() was successful. To store the output of a command
into a variable, use back-ticks:
my @ls_output = `ls -l`;
But as most of us know, you shouldn't need to do this as the following
examples do the same (except of course having the extra information the
ls provides, such as file permissions; but for that you can use the file
test operators: perldoc -f -X):
Example 1:
my @files = glob "*";
Example 2:
opendir my $fh, $dir or die "... $!\n";
my @files = grep { /^[^.]/ and -f $_ } readdir($fh);
Of course there are other ways, be careful of slurping ;)... And you
should always make sure of portability.
The "wc" command would also output information you can easily get by
just using Perl and a module or two.
.
- References:
- How to parse UNIX commands ...
- From: benbart
- How to parse UNIX commands ...
- Prev by Date: How to parse UNIX commands ...
- Next by Date: RE: How to parse UNIX commands ...
- Previous by thread: How to parse UNIX commands ...
- Next by thread: Re: How to parse UNIX commands ...
- Index(es):
Relevant Pages
|