mtimeout.1: Use correct dash for number ranges.
[misc] / hush.in
diff --git a/hush.in b/hush.in
index 4b3b751..114e6b0 100755 (executable)
--- a/hush.in
+++ b/hush.in
@@ -75,7 +75,7 @@ EOF
     *) echo >&2 "$usage"; exit 1 ;;
   esac
 done
-shift $(( OPTIND - 1 ))
+shift $(( $OPTIND - 1 ))
 
 ## Check the arguments.
 case $# in 0 | 1) echo >&2 "$usage"; exit 1 ;; esac
@@ -103,7 +103,7 @@ date=$(date +%Y-%m-%d) seq=1
 for i in "$logdir/$tag.$date#"*; do
   tail=${i##*#}
   case "$tail" in [!1-9]* | *[!0-9]*) continue ;; esac
-  if [ -f "$i" -a $tail -ge $seq ]; then seq=$(( tail + 1 )); fi
+  if [ -f "$i" -a $tail -ge $seq ]; then seq=$(( $tail + 1 )); fi
 done
 log="$logdir/$tag.$date#$seq"
 
@@ -112,7 +112,7 @@ log="$logdir/$tag.$date#$seq"
 ## example) end up giving the wrong group write permission to the file for a
 ## little bit.
 umask=$(umask)
-case ${mode+t} in t) ;; *) mode=$(printf %o $(( 0666 & ~umask ))) ;; esac
+case ${mode+t} in t) ;; *) mode=$(printf %o $(( 0666 & ~$umask ))) ;; esac
 umask 077; exec 3>"$log"; umask $umask
 case ${owner+t} in t) chown "$owner" "$log" ;; esac
 chmod $mode "$log"
@@ -145,10 +145,11 @@ EOF
 ## stage of a pipeline, where we actually wanted the status of the first.  So
 ## we write that to another pipe (fd 5) and pick it out using command
 ## substitution.
+copy () { while IFS= read -r line; do printf "%s %s\n" "$1" "$line"; done; }
 rc=$(
-  { { { { set +e; $lbuf "$cmd" "$@"; echo $? >&5; } |
-       while IFS= read line; do echo "| $line"; done >&4; } 2>&1 |
-      while IFS= read line; do echo "* $line"; done >&4; } 4>&1 |
+  { { { { set +e; $lbuf "$cmd" "$@" 3>&- 4>&- 5>&-; echo $? >&5; } |
+       copy "|" >&4; } 2>&1 |
+      copy "*" >&4; } 4>&1 |
     cat -u >&3; } 5>&1 </dev/null
 )
 
@@ -162,20 +163,21 @@ 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 ))
+  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"
-    n=$(( n - 1 ))
+  n=$(( $nlog - $maxlog ))
+  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
 fi