in reply to Re^2: Bring out your Old Perl Code
in thread Bring out your Old Perl Code

And now, I'm half way through working on scan_df but I must get out of my house in a few minutes, so I'll complete that later...

I personally believe it's ready now. Please note that my rewriting of the original script reflects my own personal preferences and is in no way intended to claim that it shows the Right™ WTDI. Also double check for errors since fundamentally I only made sure that it passes -c.

First, the original script:

#!/bin/perl -P (chdir '/usr/adm/private/memories') || die "Can't cd."; `df >newdf`; open(Df, 'olddf'); while (<Df>) { ($fs,$kbytes,$used,$avail,$capacity,$mounted_on) = split; next if $fs =~ /:/; $oldused{$fs} = $used; } open(Df, 'newdf') || die "scan_df: can't open newdf"; while (<Df>) { ($fs,$kbytes,$used,$avail,$capacity,$mounted_on) = split; next if $fs =~ /:/; $oldused = $oldused{$fs}; next if ($oldused == $used && $capacity < 99); # inactive files +ystem if ($capacity >= 90) { #if defined(mc300) || defined(mc500) || defined(mc700) $_ = substr($_,0,13) . ' ' . substr($_,13,1000); $kbytes /= 2; # translate blocks to K $used /= 2; $oldused /= 2; $avail /= 2; #endif $diff = int($used - $oldused); if ($avail < $diff * 2) { $mounted_on .= ' *'; } next if $diff < 50 && $mounted_on eq '/'; $fs =~ s|/dev/||; if ($diff >= 0) { $diff = '(+' . $diff . ')'; } else { $diff = '(' . $diff . ')'; } printf "%-8s%8d%8d %-8s%8d%7s %s\n", $fs,$kbytes,$used,$diff,$avail,$capacity,$mounted_on; } } rename('newdf','olddf');

And then, the rewriting:

#!/usr/bin/perl use strict; use warnings; use File::Basename; use Getopt::Std; my $name; BEGIN { $name = basename $0; sub USAGE () { <<".EOT"; } $name [options] Current options are: -m Assume mc300, mc500 or mc700 -h Print this help screen and exit .EOT } my %opts; getopts 'mh' => \%opts; print(USAGE), exit if $opts{h}; my $dir = '/usr/adm/private/memories'; my $olddf = 'olddf'; chdir $dir or die "[$name] Can't cd into `$dir': $!\n"; defined(my $newdf = qx/df/) or die "[$name] Can't run df: $!\n!"; my %oldused = map { my ($fs,undef,$used)=split; $fs =~ /:/ ? () : $fs => $used; } do { open my $df, '<', $olddf or die "[$name] Can't open `$olddf': $!\n +"; <$df> }; open my $df, '<', \$newdf or die "[$name] Can't open file in memory: $ +!\n"; while (<$df>) { my ($fs, $kbytes, $used, $avail, $capacity, $mounted_on) = split; next if $fs =~ /:/; my $oldused = $oldused{$fs}; next if ($oldused == $used and $capacity < 99); # inactive file +system if ($capacity >= 90) { if ($opts{m}) { substr($_,13,0) = ' ' x 8; $_ /= 2 for $kbytes, $used, $oldused, $avail; } my $diff = int($used - $oldused); $mounted_on .= ' *' if $avail < 2*$diff; next if $diff < 50 && $mounted_on eq '/'; $fs =~ s|/dev/||; $diff = '(' . ($diff >=0 ? '+' : '') . "$_)"; printf "%-8s%8d%8d %-8s%8d%7s %s\n" => $fs, $kbytes, $used, $diff, $avail, $capacity, $mounted_on; } } open my $odf, '>', $olddf or die "[$name] Can't open `$olddf': $!\n"; print $odf $newdf; __END__
--
If you can't understand the incipit, then please check the IPB Campaign.