首页 > 解决方案 > while 循环在 false 之前停止

问题描述

试图压缩所有日志,但目录中的日志除外。我知道还有其他方法可以完成我想做的事情,但我主要好奇为什么会发生以下情况。

我有以下脚本:

#!/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

要为此设置环境,您可以执行以下操作:

cd
mkdir tmp0
cd tmp0
touch test_{1..20}.log

然后将此脚本放在同一目录中(是的,是的,您也可以设置一个 var 来引用该目录)。无论如何,当这样设置并运行时,它会在$COUNT仍然小于时停止$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]$ 

您可以看到它在 2 小于 17 时停止。但是,如果在设置 tmp0 目录时,您只触摸 1 到 9 而不是两位数,则 while 循环将一直运行:

[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]$ 

我只是想知道为什么。我敢肯定这可能是愚蠢的简单,但我只是没有看到它。起初我以为是因为 macOS 上的 bash “特别”,但后来我在 Amazon Linux 上尝试了它,并且遇到了同样的事情。

提前致谢。

标签: bashloopswhile-loop

解决方案


您正在进行字符串比较。

[[ '2' > '19' ]] && echo "true"

将输出

true

使用整数算术。

(( 2 > 19 )) && echo "true"

不会输出任何东西。


推荐阅读