Montag, 28. September 2015

Comparing SunOS patches

On Solaris 10 it is sometimes necessary to compare the installed patches. The following command generates a list of all patches:
showrev -p |sed 's/^Patch: \([^ ]*\) .*/\1/'
And the following Perl script takes the output of the above command from two different systems and compares the patches. The script assumes, that the patches on the second system are newer.
#! /usr/bin/perl
use strict;
use warnings;

#showrev -p |sed 's/^Patch: \([^ ]*\) .*/\1/'

sub max {
    my $max  = shift;
    my $next = shift;
    return $max if not $next;
    unshift @_, $max > $next ? $max : $next;
    goto &max;
}

sub read_patches {
    my $file = shift;
    my $hash;
    while (<$file>) {
        s/\r?\n$//;
        my ($patch, $number) = split ('-');
        push @{$hash->{$patch}}, $number;
    }
    return $hash;
}

my ($f1, $f2) = @ARGV;

my ($fd1, $fd2);
open $fd1, "<", $f1 || die "Can not open input file $f1, $!";
open $fd2, "<", $f2 || die "Can not open input file $f2, $!";

my ($p1, $p2);
$p1 = read_patches ($fd1);
$p2 = read_patches ($fd2);

print $f1, ": ", scalar(keys %$p1), "\n";
print $f2, ": ", scalar(keys %$p2), "\n";

my ($p, $n1, $n2);
foreach $p (sort keys %$p2) {
    $n1 = max(@{$p1->{$p}});
    $n2 = max(@{$p2->{$p}});
    if (!defined $n1 || $n1 < $n2) {
        print $p, ": ", (defined $n1 ? $n1 : "()"), " -> ", $n2, "\n";
    }
}

Keine Kommentare: