Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: using grep to test a partition

by superfrink (Curate)
on Mar 08, 2007 at 05:09 UTC ( [id://603762]=note: print w/replies, xml ) Need Help??


in reply to using grep to test a partition

One way is pretty much the same:
#! /usr/bin/perl -w use strict; if ( `grep /home /etc/mtab | grep -c nfs` == 0 ) { print "Do something\n"; } else { print "DO something different\n"; }
There is nothing wrong with doing that. It does make the script depend on the "grep" program. A different way is like:
#! /usr/bin/perl -w use strict; # read the file open FH , "<" , "/etc/mtab" or die "can't open file"; my @line_list = <FH>; close FH; # grep out the "/home" lines @line_list = grep { m/\/home/ } @line_list; # search for "nfs" lines @line_list = grep { m/nfs/ } @line_list; if (scalar @line_list == 0) { print "Do something\n"; } else { print "DO something different\n"; }
I would see if I could replace both "grep"s with just one and also use a "scalar context" to get the number of matches instead of a line list.
#! /usr/bin/perl -w use strict; # read the file open FH , "<" , "/etc/mtab" or die "can't open file"; my @line_list = <FH>; close FH; # count the number of "/home" "nfs" lines my $match_count = grep { m/ \/home nfs / } @line_list; if ($match_count == 0) { print "Do something\n"; } else { print "DO something different\n"; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://603762]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2024-04-19 01:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found