home *** CD-ROM | disk | FTP | other *** search
- Xref: sparky comp.sources.wanted:3759 news.software.readers:1560 comp.lang.perl:4979 alt.sources:1770
- Path: sparky!uunet!dtix!darwin.sura.net!convex!convex!tchrist
- From: tchrist@convex.COM (Tom Christiansen)
- Newsgroups: comp.sources.wanted,news.software.readers,comp.lang.perl,alt.sources
- Subject: Re: Wanted: /bin/sh script for mantaining KILL files.
- Message-ID: <1992Jul27.154340.8482@news.eng.convex.com>
- Date: 27 Jul 92 15:43:40 GMT
- References: <1992Jul26.085354.2710@wybbs.mi.org>
- Sender: usenet@news.eng.convex.com (news access account)
- Reply-To: tchrist@convex.COM (Tom Christiansen)
- Followup-To: comp.sources.wanted,news.software.readers,comp.lang.perl,alt.sources.d
- Organization: CONVEX Realtime Development, Colorado Springs, CO
- Lines: 103
- Originator: tchrist@pixel.convex.com
- Nntp-Posting-Host: pixel.convex.com
- 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.
-
- 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.
-