###--------------------------------------------------------------------------
### Delete old log files if there are too many.
-## Count up the logfiles.
-nlog=0
+## Find out the tails of the logfile names. We assume that we're responsible
+## for all of these, and therefore that they're nicely formed.
+logs="" nlog=0
for i in "$logdir/$tag".*; do
if [ ! -f "$i" ]; then continue; fi
nlog=$(( $nlog + 1 ))
+ logs="$logs ${i#$logdir/$tag.}"
done
## If there are too many, go through and delete some early ones.
if [ $nlog -gt $maxlog ]; then
n=$(( $nlog - $maxlog ))
- for i in "$logdir/$tag".*; do
- if [ ! -f "$i" ]; then continue; fi
- rm -f "$i"
+ for i in $logs; do echo $i; done | sort -t# -k1,1 -k2n | while read i; do
+ rm -f "$logdir/$tag.$i"
n=$(( $n - 1 ))
if [ $n -eq 0 ]; then break; fi
done