Salutations:
Today at work, I received the following request:
Quick, I need to know every file that only exists on one of two servers and which server it is on as fast as possible
I started the following command on both servers, while I worked on the code:
# find / > /tmp/`hostname`
Now since I was pretty sure the requirements and formatting would change, I didn't spend any time with a 1 liner. I am not any good at them anyway. Here is what I came up with in an honest 5 minutes:
#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Std;
my %opt;
Get_Args();
open (MASTER , '<' , $opt{m}) or die "Unable to open $opt{m} as master
+ : $!";
open (SLAVE , '<' , $opt{s}) or die "Unable to open $opt{s} as slave
+: $!";
open (OUTPUT , '>' , $opt{o}) or die "Unable to open $opt{o} for outpu
+t : $!";
select OUTPUT;
my (%master , %slave);
%slave = map {chomp; $_ => undef} <SLAVE>;
while ( <MASTER> ) {
chomp;
print "$_ exists on master but not slave\n" if ! exists $slave{$_}
+;
$master{$_} = undef;
}
delete @slave{ keys %master };
print "$_ exists on slave but not master\n" for keys %slave;
sub Get_Args {
my $Usage = qq{Usage: $0 -m <master file> -s <slave file> -o <outp
+ut file>
-h : This help message.
-m : master file
-s : slave file
-o : output file
} . "\n";
getopts( 'hm:s:o:' , \%opt ) or die $Usage;
die $Usage if $opt{h} || ! $opt{m} || ! $opt{s} || ! $opt{o};
}
I keep a template file around with the Get_Args() sub in it already, so other than tweaking it, it wasn't factor.
Ok - so it did the job. Now I am wondering if I should modify it at all and, if so, how. It obviously isn't optimized to be memory efficient, but I am not sure that it needs to be. It is simple enough that maintainability isn't a concern. I have the time to work on it if I feel like it, but I also should use my time wisely.
So as I meditate on this, I would appreciate some of your insight and feedback.
In my haste, did I miss any gotchas?
What would you have come up with in the same circumstances?
Assuming my circumstances and code, what would you do now?
Cheers - L~R
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.