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.

In reply to Re^3: Bring out your Old Perl Code by blazar
in thread Bring out your Old Perl Code by Gavin

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.