chiark / gitweb /
hush.in: Fix ordering of log files so we don't delete the wrong ones.
authorMark Wooding <mdw@distorted.org.uk>
Wed, 14 Jan 2015 14:23:27 +0000 (14:23 +0000)
committerMark Wooding <mdw@distorted.org.uk>
Wed, 14 Jan 2015 14:28:05 +0000 (14:28 +0000)
The sequence numbers aren't lexicographically ordered, so call out to
sort(1) to put them in the right order.

hush.in

diff --git a/hush.in b/hush.in
index 2752cdf7e045065a78a7cc8d1dc044846aa1be3e..f10f5070ab50eb8689052aa8be4aaa7ac6d3a8d3 100755 (executable)
--- a/hush.in
+++ b/hush.in
@@ -163,19 +163,20 @@ exec 3>&-
 ###--------------------------------------------------------------------------
 ### 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