Re: How to parse UNIX commands ...



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.

.



Relevant Pages

  • Re: parsing a response line from sysfiletree
    ... Parse value subwordsubword ... This comes from using a machine with dots as the 'directory' separator a lot ... File$Type_C85 has value "JPEG" ... to find the command to be used to 'run' a jpeg. ...
    (comp.lang.rexx)
  • Re: OptionParser Question
    ... -- BTR ... I'm using OptionParser to parse command line options. ... puts "The arguments are #" ...
    (comp.lang.ruby)
  • Re: Need to start mdb using Startup command-line options and pass in a parameter
    ... Then in the startup code for the database you would parse the Command ... > 'now execute the command in the .net app. ...
    (microsoft.public.access.formscoding)
  • Re: Parsing a bash string?
    ... seguso wrote in message news: ... > I have to be able to parse a command line typed by the user. ... > I.e., I need a function that, when given a typical bash command string like ...
    (comp.unix.shell)
  • Re: Retrieve most recent file
    ... command to loop through and parse the version number out so you can ... retrieve the file "...\CAMTA_be Vxx Ryy.mdb" where xx and yy would be the ...
    (microsoft.public.access.modulesdaovba)