🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: news.software.readers
Article #1934

Re: Wanted: /bin/sh script for mantaining KILL files.

#1934
From: Tom Christiansen
Date: Mon, 27 Jul 1992 15:43
104 lines
4237 bytes
Archive-name: killkill
Submitter: Tom Christiansen <tchrist@convex.com>

From the keyboard of an unknown person @wybbs.mi.org:
:I would like to get my hands on a script to do
:some or all of the following.
: 
:1> Look for all kill files over X-bytes in length
:1a> trim the file down until it's under X-bytes, by removing
:    the first line(s) of kill commands (leaving the
:    THRU line intact.
:2> Remove KILL files that are older than X-days since 
:    last access.
:3> Mail a report to the user on which files were cleaned
:    up, and what was taken out, or which files were
:    deleted.
:4> Can be used either system-wide using cron, or
:    on an individual basis, like in .profile
: 
:Ok, folks... that's what I would like. I would do
:it myself, but I'm still not fluent in /bin/sh scripts
:to get it to work. (Plus, looking at the script or
:source if U care to make it compilable would be very
:useful to show me how to incorporate routines into
:other code as well.)
: 
:I am using SCO-Xenix SysV-386.

Here's a Perl solution.  I'll leave the running from cron
or sending mail as an exercise for the reader.

--tom

#!/usr/bin/perl
#
# killkill -- maintain killfiles by size and age
# tchrist@convex.com    Mon Jul 27 10:35:44 CDT 1992
#
# arguments are considered user names, defaulting
# to the current user if not given.
#
# for all killfiles in a user's newsdirectory that are older than
# $MAXDAYS, unlink them, or if bigger than $MAXSIZE, save the THRU line
# and discard lines (oldest first) until size is under $MAXSIZE.  
#
# a killfile is defined by the pattern $KILLPAT; a newsdir is a
# directory residing in ~$user/$NEWSDIR.
#
# print out each killfile found, and what if anything is done to it.
# if $DEBUG is true, don't really touch any files.

require 'find.pl';

$MAXSIZE = 250;
$MAXDAYS = 30;
$KILLPAT = '^KILL$';
$NEWSDIR = 'News';
$DEBUG = 0;

if (@ARGV == 0) { # no args means default to current user
    @ARGV = (       $ENV{USER}
		 || $ENV{LOGNAME}
		 || (getpwuid($>))[0]
		 || die "Cannot determine user for id $>"
	    );
} 

print "$0: maxsize is $MAXSIZE, maxdays is $MAXDAYS, debug is $DEBUG\n";

while ($user = shift) {
    $home = (getpwnam($user))[7];
    $newsdir = "$home/$NEWSDIR";
    next unless -d $newsdir;
    &find($newsdir);
} 

sub wanted {
    /$KILLPAT/o || return;
    $size = -s $_;
    $age  = -M _;  
    printf "%-50s  age = %5.1f   size = %d\n", $name, $age, $size;
    if ($age > $MAXDAYS) {
	print "\tunlinking $name\n";
	unlink unless $DEBUG;
    } elsif ($size > $MAXSIZE) {
	print "\ttrimming $name\n";
	unless ($DEBUG) {
	    ($thru, @lines) = `cat $name`;
	    shift @lines until length("$thru @lines") < $MAXSIZE;
	    open (KF, ">$name") || die "can't write $name: $!";
	    print KF $thru, @lines;
	    close KF || die "can't close $name: $!"
	}
    } 
} 
__END__


-- 
    Tom Christiansen      tchrist@convex.com      convex!tchrist


    /earth is 98% full ... please delete anyone you can.
Technical Details
Message-ID: <1992Jul27.154340.8482@news.eng.convex.com>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!i2pn2.pugleaf.net!archive.newsdeef.eu!mbox2nntp!wupost!sdd.hp.com!mips!darwin.sura.net!convex!convex!tchrist
References: <1992Jul26.085354.2710@wybbs.mi.org>

Show Full Headers
Complete Article Headers
Message-ID: <1992Jul27.154340.8482@news.eng.convex.com>
Subject: Re: Wanted: /bin/sh script for mantaining KILL files.
Date: Mon, 27 Jul 1992 15:43:40 GMT
From: Tom Christiansen <tchrist@convex.COM>
References: <1992Jul26.085354.2710@wybbs.mi.org>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!i2pn2.pugleaf.net!archive.newsdeef.eu!mbox2nntp!wupost!sdd.hp.com!mips!darwin.sura.net!convex!convex!tchrist
Newsgroups: comp.sources.wanted,news.software.readers,comp.lang.perl,alt.sources
Followup-To: comp.sources.wanted,news.software.readers,comp.lang.perl,alt.sources.d
Originator: tchrist@pixel.convex.com
Sender: usenet@news.eng.convex.com (news access account)
Nntp-Posting-Host: pixel.convex.com
Reply-To: tchrist@convex.COM (Tom Christiansen)
Organization: CONVEX Realtime Development, Colorado Springs, CO
X-Disclaimer: This message was written by a user at CONVEX Computer
  Corp. The opinions expressed are those of the user and
  not necessarily those of CONVEX.
Lines: 103