#!/bin/ksh # # $Id: keep,v 1.2 2002/02/05 22:05:55 vogelke Exp $ # # NAME: # keep # # SYNOPSIS: # keep [-hv] count dir [regex] # # DESCRIPTION: # Preserves the last "count" files under directory "dir" # which match optional regular expression "regex". # # OPTIONS: # -h, --help print this message # -v, --version print the version and exit # # AUTHOR: # Based on Free Software Foundation configure scripts. # # Karl Vogel # Sumaria Systems, Inc. PATH=/bin:/usr/sbin:/usr/bin export PATH umask 022 tag=`basename $0` # ======================== FUNCTIONS ============================= # die: prints an optional argument to stderr and exits. # A common use for "die" is with a test: # test -f /etc/passwd || die "no passwd file" # This works in subshells and loops, but may not exit with # a code other than 0. die () { echo "$tag: error: $*" 1>&2 exit 1 } # usage: prints an optional string plus part of the comment # header (if any) to stderr, and exits with code 1. usage () { lines=`egrep -n '^# (NAME|AUTHOR)' $0 | sed -e 's/:.*//'` ( case "$#" in 0) ;; *) echo "usage error: $*"; echo ;; esac case "$lines" in "") ;; *) set `echo $lines | sed -e 's/ /,/'` sed -n ${1}p $0 | sed -e 's/^#//g' | egrep -v AUTHOR: ;; esac ) 1>&2 exit 1 } # version: prints the current version to stdout. version () { lsedscr='s/RCSfile: // s/.Date: // s/,v . .Revision: / v/ s/\$//g' lrevno='$RCSfile: keep,v $ $Revision: 1.2 $' lrevdate='$Date: 2002/02/05 22:05:55 $' echo "$lrevno $lrevdate" | sed -e "$lsedscr" exit 0 } # ======================== MAIN PROGRAM ========================== # Defaults: ac_invalid="invalid option; use --help to show usage" count= dir= regex='.' k=0 for ac_option do case "$ac_option" in -h | -help | --help | --hel | --he) usage ;; -v | -version | --version | --versio |\ --versi | --vers) version ;; -*) die "$ac_option: $ac_invalid" ;; *) k=`expr $k + 1` case "$k" in 1) count="$ac_option" ;; 2) dir="$ac_option" ;; 3) regex="$ac_option" ;; *) usage "too many args" ;; esac ;; esac done # # Real work starts here. # case "$count" in "") usage "no file count entered" ;; [1-9]*) count=`expr $count + 1`;; *) usage "file count must be a number" ;; esac case "$dir" in "") usage "no directory entered" ;; *) ;; esac test -d "$dir" || die "$dir: not a directory" cd $dir set X `ls -1t 2> /dev/null | grep "$regex" | tail +"$count"` shift case "$#" in 0) ;; *) rm $* ;; esac exit 0