Shell Strange Bash problem

While debugging a script I wrote for backups I noticed some strange problems.
I snipped out the log directory sanity part of the script to show you and I'm hoping
someone can shed some light on this...

Code:
root@Washington:/usr/home/tim.falardeau/LOGS # ls
20160806.log  20160813.log  20160820.log  20160827.log  2016093.log
20160807.log  20160814.log  20160821.log  20160828.log  2016094.log
20160808.log  20160815.log  20160822.log  20160829.log  2016095.log
20160809.log  20160816.log  20160823.log  20160830.log  2016096.log
20160810.log  20160817.log  20160824.log  20160831.log  2016097.log
20160811.log  20160818.log  20160825.log  2016091.log  2016098.log
20160812.log  20160819.log  20160826.log  2016092.log  2016099.log

I have 35 log files in the directory all with an appropriate date stamp...
This is the code with the issue...

Code:
root@Washington:/usr/home/tim.falardeau/LOGS # cat ~/bin/test.sh
#! /usr/local/bin/bash

if [[ $1 = "-d" ]]; then
  set -x
fi

NUMLOGS=9
LOGDIR="/usr/home/tim.falardeau/LOGS"
TMPLOG="/usr/home/tim.falardeau/tempfile"

  FILES=($(ls $LOGDIR | grep log | grep -v grep))

func(){
  if [[ ${#FILES[@]} > $NUMLOGS ]]; then
  echo "** Deleting log files for dir sanity" >> $TMPLOG
  NUM=$(( ${#FILES[@]} - $NUMLOGS ))
  RMLIST=($(ls $LOGDIR | grep log | grep -v grep | tail -n $NUM))
  for ((x=0; x<${#RMLIST[@]}; x++)); do
  local ERR="$(rm $LOGDIR/${RMLIST[$x]})"
  if [[ $? != 0 ]]; then
  echo "!! Error deleting ${RMLIST[$x]} with error $ERR" >> $TMPLOG
  else
  echo "-> Successfully deleted ${RMLIST[$x]}" >> $TMPLOG
  fi
  done
  fi
}

func

exit 0

Real basic stuff... And everything works like a charm until I set the variable NUMLOGS, which is the number of log files to keep, to a single digit number. When NUMLOGS is set to a double digit number, everything is fine.

EG:
35 files in the dir.
Case 1: NUMLOGS=20
Code:
root@Washington:/usr/home/tim.falardeau/LOGS # test.sh -d
+ NUMLOGS=20
+ LOGDIR=/usr/home/tim.falardeau/LOGS
+ TMPLOG=/usr/home/tim.falardeau/tempfile
+ FILES=($(ls $LOGDIR | grep log | grep -v grep))
++ ls /usr/home/tim.falardeau/LOGS
++ grep log
++ grep -v grep
+ func
+ [[ 35 > 20 ]]
+ echo '** Deleting log files for dir sanity'
+ NUM=15
+ RMLIST=($(ls $LOGDIR | grep log | grep -v grep | tail -n $NUM))
++ ls /usr/home/tim.falardeau/LOGS
++ grep log
++ grep -v grep
++ tail -n 15
+ (( x=0 ))
+ (( x<15 ))
++ rm /usr/home/tim.falardeau/LOGS/20160826.log
+ local ERR=
+ [[ 0 != 0 ]]
+ echo '-> Successfully deleted 20160826.log'
+ (( x++ ))
+ (( x<15 ))
++ rm /usr/home/tim.falardeau/LOGS/20160827.log
+ local ERR=
+ [[ 0 != 0 ]]
+ echo '-> Successfully deleted 20160827.log'
+ (( x++ ))
+ (( x<15 ))
++ rm /usr/home/tim.falardeau/LOGS/20160828.log
+ local ERR=
+ [[ 0 != 0 ]]
+ echo '-> Successfully deleted 20160828.log'
+ (( x++ ))
+ (( x<15 ))
++ rm /usr/home/tim.falardeau/LOGS/20160829.log
+ local ERR=
+ [[ 0 != 0 ]]
+ echo '-> Successfully deleted 20160829.log'
+ (( x++ ))
+ (( x<15 ))
++ rm /usr/home/tim.falardeau/LOGS/20160830.log
+ local ERR=
+ [[ 0 != 0 ]]
+ echo '-> Successfully deleted 20160830.log'
+ (( x++ ))
+ (( x<15 ))
++ rm /usr/home/tim.falardeau/LOGS/20160831.log
+ local ERR=
+ [[ 0 != 0 ]]
+ echo '-> Successfully deleted 20160831.log'
+ (( x++ ))
+ (( x<15 ))
++ rm /usr/home/tim.falardeau/LOGS/2016091.log
+ local ERR=
+ [[ 0 != 0 ]]
+ echo '-> Successfully deleted 2016091.log'
+ (( x++ ))
+ (( x<15 ))
++ rm /usr/home/tim.falardeau/LOGS/2016092.log
+ local ERR=
+ [[ 0 != 0 ]]
+ echo '-> Successfully deleted 2016092.log'
+ (( x++ ))
+ (( x<15 ))
++ rm /usr/home/tim.falardeau/LOGS/2016093.log
+ local ERR=
+ [[ 0 != 0 ]]
+ echo '-> Successfully deleted 2016093.log'
+ (( x++ ))
+ (( x<15 ))
++ rm /usr/home/tim.falardeau/LOGS/2016094.log
+ local ERR=
+ [[ 0 != 0 ]]
+ echo '-> Successfully deleted 2016094.log'
+ (( x++ ))
+ (( x<15 ))
++ rm /usr/home/tim.falardeau/LOGS/2016095.log
+ local ERR=
+ [[ 0 != 0 ]]
+ echo '-> Successfully deleted 2016095.log'
+ (( x++ ))
+ (( x<15 ))
++ rm /usr/home/tim.falardeau/LOGS/2016096.log
+ local ERR=
+ [[ 0 != 0 ]]
+ echo '-> Successfully deleted 2016096.log'
+ (( x++ ))
+ (( x<15 ))
++ rm /usr/home/tim.falardeau/LOGS/2016097.log
+ local ERR=
+ [[ 0 != 0 ]]
+ echo '-> Successfully deleted 2016097.log'
+ (( x++ ))
+ (( x<15 ))
++ rm /usr/home/tim.falardeau/LOGS/2016098.log
+ local ERR=
+ [[ 0 != 0 ]]
+ echo '-> Successfully deleted 2016098.log'
+ (( x++ ))
+ (( x<15 ))
++ rm /usr/home/tim.falardeau/LOGS/2016099.log
+ local ERR=
+ [[ 0 != 0 ]]
+ echo '-> Successfully deleted 2016099.log'
+ (( x++ ))
+ (( x<15 ))
+ exit 0

Case 2: NUMLOGS=10
Code:
root@Washington:/usr/home/tim.falardeau/LOGS # test.sh -d
+ NUMLOGS=10
+ LOGDIR=/usr/home/tim.falardeau/LOGS
+ TMPLOG=/usr/home/tim.falardeau/tempfile
+ FILES=($(ls $LOGDIR | grep log | grep -v grep))
++ ls /usr/home/tim.falardeau/LOGS
++ grep log
++ grep -v grep
+ func
+ [[ 20 > 10 ]]
+ echo '** Deleting log files for dir sanity'
+ NUM=10
+ RMLIST=($(ls $LOGDIR | grep log | grep -v grep | tail -n $NUM))
++ ls /usr/home/tim.falardeau/LOGS
++ grep log
++ grep -v grep
++ tail -n 10
+ (( x=0 ))
+ (( x<10 ))
++ rm /usr/home/tim.falardeau/LOGS/20160816.log
+ local ERR=
+ [[ 0 != 0 ]]
+ echo '-> Successfully deleted 20160816.log'
+ (( x++ ))
+ (( x<10 ))
++ rm /usr/home/tim.falardeau/LOGS/20160817.log
+ local ERR=
+ [[ 0 != 0 ]]
+ echo '-> Successfully deleted 20160817.log'
+ (( x++ ))
+ (( x<10 ))
++ rm /usr/home/tim.falardeau/LOGS/20160818.log
+ local ERR=
+ [[ 0 != 0 ]]
+ echo '-> Successfully deleted 20160818.log'
+ (( x++ ))
+ (( x<10 ))
++ rm /usr/home/tim.falardeau/LOGS/20160819.log
+ local ERR=
+ [[ 0 != 0 ]]
+ echo '-> Successfully deleted 20160819.log'
+ (( x++ ))
+ (( x<10 ))
++ rm /usr/home/tim.falardeau/LOGS/20160820.log
+ local ERR=
+ [[ 0 != 0 ]]
+ echo '-> Successfully deleted 20160820.log'
+ (( x++ ))
+ (( x<10 ))
++ rm /usr/home/tim.falardeau/LOGS/20160821.log
+ local ERR=
+ [[ 0 != 0 ]]
+ echo '-> Successfully deleted 20160821.log'
+ (( x++ ))
+ (( x<10 ))
++ rm /usr/home/tim.falardeau/LOGS/20160822.log
+ local ERR=
+ [[ 0 != 0 ]]
+ echo '-> Successfully deleted 20160822.log'
+ (( x++ ))
+ (( x<10 ))
++ rm /usr/home/tim.falardeau/LOGS/20160823.log
+ local ERR=
+ [[ 0 != 0 ]]
+ echo '-> Successfully deleted 20160823.log'
+ (( x++ ))
+ (( x<10 ))
++ rm /usr/home/tim.falardeau/LOGS/20160824.log
+ local ERR=
+ [[ 0 != 0 ]]
+ echo '-> Successfully deleted 20160824.log'
+ (( x++ ))
+ (( x<10 ))
++ rm /usr/home/tim.falardeau/LOGS/20160825.log
+ local ERR=
+ [[ 0 != 0 ]]
+ echo '-> Successfully deleted 20160825.log'
+ (( x++ ))
+ (( x<10 ))
+ exit 0

Case 3: NUMLOGS=9
Code:
root@Washington:/usr/home/tim.falardeau/LOGS # test.sh -d
+ NUMLOGS=9
+ LOGDIR=/usr/home/tim.falardeau/LOGS
+ TMPLOG=/usr/home/tim.falardeau/tempfile
+ FILES=($(ls $LOGDIR | grep log | grep -v grep))
++ ls /usr/home/tim.falardeau/LOGS
++ grep log
++ grep -v grep
+ func
+ [[ 10 > 9 ]]
+ exit 0

Anyone have any idea what this is all about?
It's not really that big a deal considering that I want to keep 30 days of logs on hand anyways. But, I could see where knowing why this happens could help people from tripping over their own code in the future...
 
The answer lies deep within bash(1), in the "CONDITIONAL EXPRESSIONS" section:


When used with [[, the < and > operators sort lexicographically using
the current locale. The test command sorts using ASCII ordering.

string1 < string2
True if string1 sorts before string2 lexicographically.

string1 > string2
True if string1 sorts after string2 lexicographically.

arg1 OP arg2
OP is one of -eq, -ne, -lt, -le, -gt, or -ge. These arithmetic
binary operators return true if arg1 is equal to, not equal to,
less than, less than or equal to, greater than, or greater than
or equal to arg2, respectively. Arg1 and arg2 may be positive
or negative integers.
 
Thanks all...
I've said it once, and I'll say it again...
The community is 90% the reason FreeBSD is best...
 
Back
Top