Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

uniq_exec.pl - filters duplicate program output

by diotalevi (Canon)
on Jul 18, 2003 at 20:12 UTC ( [id://275739]=sourcecode: print w/replies, xml ) Need Help??
Category: Utility Scripts
Author/Contact Info jjore@cpan.org
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 $?";

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-04-25 18:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found