Trying to zip up all logs except for one in a directory. I know there are other ways to accomplish what I'm trying to do, but I'm mainly curious as to WHY the following is happening.
I have the following script:
#!/usr/bin/env bash
TOTAL=$(ls -lah ./*.log | wc -l)
let "TOTAL--"
COUNT=0
while [[ $COUNT < $TOTAL ]]; do
gzip $(ls -1 | grep -v '.gz' | grep '.log' | head -n 1)
let "COUNT++"
echo "$TOTAL - $COUNT = $(($TOTAL-$COUNT))"
sleep 2
done
To set up the environment for this, you could do something like:
cd
mkdir tmp0
cd tmp0
touch test_{1..20}.log
Then put this script inside the same directory (yes, yes you could also just set a var to reference the directory too). Anyways, when set up like this and run, it stops while $COUNT
is still less than $TOTAL
:
[ec2-user@ip-172-31-0-70 ~]$ cd
[ec2-user@ip-172-31-0-70 ~]$ mkdir tmp0
[ec2-user@ip-172-31-0-70 ~]$ cd tmp0/
[ec2-user@ip-172-31-0-70 tmp0]$ touch test_{1..20}.log
[ec2-user@ip-172-31-0-70 tmp0]$ mv ../myscript.sh .
[ec2-user@ip-172-31-0-70 tmp0]$ ll
total 4
-rwxrwxr-x 1 ec2-user ec2-user 245 Aug 9 11:45 myscript.sh
-rw-rw-r-- 1 ec2-user ec2-user 0 Aug 9 12:24 test_10.log
-rw-rw-r-- 1 ec2-user ec2-user 0 Aug 9 12:24 test_11.log
-rw-rw-r-- 1 ec2-user ec2-user 0 Aug 9 12:24 test_12.log
-rw-rw-r-- 1 ec2-user ec2-user 0 Aug 9 12:24 test_13.log
-rw-rw-r-- 1 ec2-user ec2-user 0 Aug 9 12:24 test_14.log
-rw-rw-r-- 1 ec2-user ec2-user 0 Aug 9 12:24 test_15.log
-rw-rw-r-- 1 ec2-user ec2-user 0 Aug 9 12:24 test_16.log
-rw-rw-r-- 1 ec2-user ec2-user 0 Aug 9 12:24 test_17.log
-rw-rw-r-- 1 ec2-user ec2-user 0 Aug 9 12:24 test_18.log
-rw-rw-r-- 1 ec2-user ec2-user 0 Aug 9 12:24 test_19.log
-rw-rw-r-- 1 ec2-user ec2-user 0 Aug 9 12:24 test_1.log
-rw-rw-r-- 1 ec2-user ec2-user 0 Aug 9 12:24 test_20.log
-rw-rw-r-- 1 ec2-user ec2-user 0 Aug 9 12:24 test_2.log
-rw-rw-r-- 1 ec2-user ec2-user 0 Aug 9 12:24 test_3.log
-rw-rw-r-- 1 ec2-user ec2-user 0 Aug 9 12:24 test_4.log
-rw-rw-r-- 1 ec2-user ec2-user 0 Aug 9 12:24 test_5.log
-rw-rw-r-- 1 ec2-user ec2-user 0 Aug 9 12:24 test_6.log
-rw-rw-r-- 1 ec2-user ec2-user 0 Aug 9 12:24 test_7.log
-rw-rw-r-- 1 ec2-user ec2-user 0 Aug 9 12:24 test_8.log
-rw-rw-r-- 1 ec2-user ec2-user 0 Aug 9 12:24 test_9.log
[ec2-user@ip-172-31-0-70 tmp0]$ ./myscript.sh
19 - 1 = 18
19 - 2 = 17
[ec2-user@ip-172-31-0-70 tmp0]$ ll
total 12
-rwxrwxr-x 1 ec2-user ec2-user 245 Aug 9 11:45 myscript.sh
-rw-rw-r-- 1 ec2-user ec2-user 32 Aug 9 12:24 test_10.log.gz
-rw-rw-r-- 1 ec2-user ec2-user 32 Aug 9 12:24 test_11.log.gz
-rw-rw-r-- 1 ec2-user ec2-user 0 Aug 9 12:24 test_12.log
-rw-rw-r-- 1 ec2-user ec2-user 0 Aug 9 12:24 test_13.log
-rw-rw-r-- 1 ec2-user ec2-user 0 Aug 9 12:24 test_14.log
-rw-rw-r-- 1 ec2-user ec2-user 0 Aug 9 12:24 test_15.log
-rw-rw-r-- 1 ec2-user ec2-user 0 Aug 9 12:24 test_16.log
-rw-rw-r-- 1 ec2-user ec2-user 0 Aug 9 12:24 test_17.log
-rw-rw-r-- 1 ec2-user ec2-user 0 Aug 9 12:24 test_18.log
-rw-rw-r-- 1 ec2-user ec2-user 0 Aug 9 12:24 test_19.log
-rw-rw-r-- 1 ec2-user ec2-user 0 Aug 9 12:24 test_1.log
-rw-rw-r-- 1 ec2-user ec2-user 0 Aug 9 12:24 test_20.log
-rw-rw-r-- 1 ec2-user ec2-user 0 Aug 9 12:24 test_2.log
-rw-rw-r-- 1 ec2-user ec2-user 0 Aug 9 12:24 test_3.log
-rw-rw-r-- 1 ec2-user ec2-user 0 Aug 9 12:24 test_4.log
-rw-rw-r-- 1 ec2-user ec2-user 0 Aug 9 12:24 test_5.log
-rw-rw-r-- 1 ec2-user ec2-user 0 Aug 9 12:24 test_6.log
-rw-rw-r-- 1 ec2-user ec2-user 0 Aug 9 12:24 test_7.log
-rw-rw-r-- 1 ec2-user ec2-user 0 Aug 9 12:24 test_8.log
-rw-rw-r-- 1 ec2-user ec2-user 0 Aug 9 12:24 test_9.log
[ec2-user@ip-172-31-0-70 tmp0]$
[ec2-user@ip-172-31-0-70 tmp0]$
[ec2-user@ip-172-31-0-70 tmp0]$ bash -x ./myscript.sh
++ wc -l
++ ls -lah ./test_12.log ./test_13.log ./test_14.log ./test_15.log ./test_16.log ./test_17.log ./test_18.log ./test_19.log ./test_1.log ./test_20.log ./test_2.log ./test_3.log ./test_4.log ./test_5.log ./test_6.log ./test_7.log ./test_8.log ./test_9.log
+ TOTAL=18
+ let TOTAL--
+ COUNT=0
+ [[ 0 < 17 ]]
++ head -n 1
++ grep .log
++ grep -v .gz
++ ls -1
+ gzip test_12.log
+ let COUNT++
+ echo '17 - 1 = 16'
17 - 1 = 16
+ sleep 2
+ [[ 1 < 17 ]]
++ head -n 1
++ grep .log
++ grep -v .gz
++ ls -1
+ gzip test_13.log
+ let COUNT++
+ echo '17 - 2 = 15'
17 - 2 = 15
+ sleep 2
+ [[ 2 < 17 ]]
[ec2-user@ip-172-31-0-70 tmp0]$
You can see that it stops while 2 is less than 17. But, if when setting up the tmp0 directory, you only touch 1 through 9 instead of double digits, the while loop runs all the way through:
[ec2-user@ip-172-31-0-70 tmp0]$ rm test*
[ec2-user@ip-172-31-0-70 tmp0]$ ll
total 4
-rwxrwxr-x 1 ec2-user ec2-user 245 Aug 9 11:45 myscript.sh
[ec2-user@ip-172-31-0-70 tmp0]$ touch test_{1..9}.log
[ec2-user@ip-172-31-0-70 tmp0]$ ll
total 4
-rwxrwxr-x 1 ec2-user ec2-user 245 Aug 9 11:45 myscript.sh
-rw-rw-r-- 1 ec2-user ec2-user 0 Aug 9 12:28 test_1.log
-rw-rw-r-- 1 ec2-user ec2-user 0 Aug 9 12:28 test_2.log
-rw-rw-r-- 1 ec2-user ec2-user 0 Aug 9 12:28 test_3.log
-rw-rw-r-- 1 ec2-user ec2-user 0 Aug 9 12:28 test_4.log
-rw-rw-r-- 1 ec2-user ec2-user 0 Aug 9 12:28 test_5.log
-rw-rw-r-- 1 ec2-user ec2-user 0 Aug 9 12:28 test_6.log
-rw-rw-r-- 1 ec2-user ec2-user 0 Aug 9 12:28 test_7.log
-rw-rw-r-- 1 ec2-user ec2-user 0 Aug 9 12:28 test_8.log
-rw-rw-r-- 1 ec2-user ec2-user 0 Aug 9 12:28 test_9.log
[ec2-user@ip-172-31-0-70 tmp0]$ bash -x ./myscript.sh
++ wc -l
++ ls -lah ./test_1.log ./test_2.log ./test_3.log ./test_4.log ./test_5.log ./test_6.log ./test_7.log ./test_8.log ./test_9.log
+ TOTAL=9
+ let TOTAL--
+ COUNT=0
+ [[ 0 < 8 ]]
++ head -n 1
++ grep .log
++ grep -v .gz
++ ls -1
+ gzip test_1.log
+ let COUNT++
+ echo '8 - 1 = 7'
8 - 1 = 7
+ sleep 2
+ [[ 1 < 8 ]]
++ head -n 1
++ grep .log
++ grep -v .gz
++ ls -1
+ gzip test_2.log
+ let COUNT++
+ echo '8 - 2 = 6'
8 - 2 = 6
+ sleep 2
+ [[ 2 < 8 ]]
++ head -n 1
++ grep .log
++ grep -v .gz
++ ls -1
+ gzip test_3.log
+ let COUNT++
+ echo '8 - 3 = 5'
8 - 3 = 5
+ sleep 2
+ [[ 3 < 8 ]]
++ head -n 1
++ grep .log
++ grep -v .gz
++ ls -1
+ gzip test_4.log
+ let COUNT++
+ echo '8 - 4 = 4'
8 - 4 = 4
+ sleep 2
+ [[ 4 < 8 ]]
++ head -n 1
++ grep .log
++ grep -v .gz
++ ls -1
+ gzip test_5.log
+ let COUNT++
+ echo '8 - 5 = 3'
8 - 5 = 3
+ sleep 2
+ [[ 5 < 8 ]]
++ head -n 1
++ grep .log
++ grep -v .gz
++ ls -1
+ gzip test_6.log
+ let COUNT++
+ echo '8 - 6 = 2'
8 - 6 = 2
+ sleep 2
+ [[ 6 < 8 ]]
++ head -n 1
++ grep .log
++ grep -v .gz
++ ls -1
+ gzip test_7.log
+ let COUNT++
+ echo '8 - 7 = 1'
8 - 7 = 1
+ sleep 2
+ [[ 7 < 8 ]]
++ head -n 1
++ grep .log
++ grep -v .gz
++ ls -1
+ gzip test_8.log
+ let COUNT++
+ echo '8 - 8 = 0'
8 - 8 = 0
+ sleep 2
+ [[ 8 < 8 ]]
[ec2-user@ip-172-31-0-70 tmp0]$ ll
total 36
-rwxrwxr-x 1 ec2-user ec2-user 245 Aug 9 11:45 myscript.sh
-rw-rw-r-- 1 ec2-user ec2-user 31 Aug 9 12:28 test_1.log.gz
-rw-rw-r-- 1 ec2-user ec2-user 31 Aug 9 12:28 test_2.log.gz
-rw-rw-r-- 1 ec2-user ec2-user 31 Aug 9 12:28 test_3.log.gz
-rw-rw-r-- 1 ec2-user ec2-user 31 Aug 9 12:28 test_4.log.gz
-rw-rw-r-- 1 ec2-user ec2-user 31 Aug 9 12:28 test_5.log.gz
-rw-rw-r-- 1 ec2-user ec2-user 31 Aug 9 12:28 test_6.log.gz
-rw-rw-r-- 1 ec2-user ec2-user 31 Aug 9 12:28 test_7.log.gz
-rw-rw-r-- 1 ec2-user ec2-user 31 Aug 9 12:28 test_8.log.gz
-rw-rw-r-- 1 ec2-user ec2-user 0 Aug 9 12:28 test_9.log
[ec2-user@ip-172-31-0-70 tmp0]$ bash -x ./myscript.sh
++ wc -l
++ ls -lah ./test_9.log
+ TOTAL=1
+ let TOTAL--
+ COUNT=0
+ [[ 0 < 0 ]]
[ec2-user@ip-172-31-0-70 tmp0]$ ll
total 36
-rwxrwxr-x 1 ec2-user ec2-user 245 Aug 9 11:45 myscript.sh
-rw-rw-r-- 1 ec2-user ec2-user 31 Aug 9 12:28 test_1.log.gz
-rw-rw-r-- 1 ec2-user ec2-user 31 Aug 9 12:28 test_2.log.gz
-rw-rw-r-- 1 ec2-user ec2-user 31 Aug 9 12:28 test_3.log.gz
-rw-rw-r-- 1 ec2-user ec2-user 31 Aug 9 12:28 test_4.log.gz
-rw-rw-r-- 1 ec2-user ec2-user 31 Aug 9 12:28 test_5.log.gz
-rw-rw-r-- 1 ec2-user ec2-user 31 Aug 9 12:28 test_6.log.gz
-rw-rw-r-- 1 ec2-user ec2-user 31 Aug 9 12:28 test_7.log.gz
-rw-rw-r-- 1 ec2-user ec2-user 31 Aug 9 12:28 test_8.log.gz
-rw-rw-r-- 1 ec2-user ec2-user 0 Aug 9 12:28 test_9.log
[ec2-user@ip-172-31-0-70 tmp0]$
I'm just wondering why. I'm sure it's probably something stupid simple, but I'm just not seeing it. At first I thought it was because bash on macOS was "special", but then I tried it on Amazon Linux, and experienced the same thing.
Thanks in advance.