Description: |
This is sort of like uniq for programs - the script only prints your program's output when it is different than the last time. I wrote this so I could put `whois -i somedomain.org` in a cron job and only get notices when Network Solutions' database changes. You could use this with lynx or wget to see when a web page changes, etc. I assume this already some common unix tool so please let me know which one that is, I only wrote this because I didn't know what the tool was called. |
#!/usr/bin/perl
use strict;
use warnings;
use File::Spec;
use Cache::FileCache ();
unless ( @ARGV ) {
my $script = (File::Spec->splitpath($0))[2];
die "$script another_program -and -its -args\n"
. "caches the output from the command and only prints if it diff
+ers fro"
. "m the last time $script was run. This allows you to say `$scr
+ipt who"
. "is -i somedomain.org` and only have output show up when the w
+hois ou"
. "tput changes\n";
}
my $pid;
my $sleep_count = 0;
do {
$pid = open KID_TO_READ, "-|", @ARGV;
unless (defined $pid) {
warn "cannot fork: $!";
die "bailing out" if $sleep_count++ > 6;
sleep 10;
}
} until defined $pid;
my $self = File::Spec->rel2abs( $0 );
my $cache = Cache::FileCache->new( { namespace => $self } );
my $old = $cache->get( "@ARGV" );
my $new = do { local $/; <KID_TO_READ> };
if ($new ne $old) {
print $new;
$cache->set( "@ARGV", $new );
}
close KID_TO_READ or warn "kid exited $?";
|