#!/bin/ksh # # Roll log files. # This script will maintain a specified number of old log files, # named by appending a number to the end of it, with the older # files having higher numbers. Numbering will start at zero. # # Andy Welter # www.the-welters.com # 1/4/2001 # stat_check () { RC=$1 MSG="$2" if [ "$RC" != "0" ]; then print "ERROR: $MSG" exit $RC fi } FILE=$1 if [ ! -f "$FILE" ]; then stat_check 1 "No such log file $FILE" fi if [ $# -eq 1 ]; then COUNT=6 else COUNT=`expr $2 - 1` fi CUR=$COUNT while [ $COUNT -ge 1 ]; do NEXT=`expr $COUNT - 1` if [ -f $FILE.$COUNT ]; then rm $FILE.$COUNT stat_check $? "Removing $FILE.$COUNT" fi if [ -f $FILE.$NEXT ]; then mv $FILE.$NEXT $FILE.$COUNT stat_check $? "Moving $FILE.$NEXT" fi COUNT=`expr $COUNT - 1` done # # Copy then truncate the active log file to avoid problems with # open file descriptors cp -p $FILE $FILE.0 stat_check $? "Moving $FILE.$NEXT" cat /dev/null > $FILE