while循环在错误之前停止

时间:2018-08-09 19:36:01

标签: bash loops while-loop

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

我有以下脚本:

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

然后将该脚本放在同一目录中(是的,是的,您也可以只设置一个变量来引用该目录)。无论如何,当这样设置并运行时,它会在$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上尝试了一下,并经历了同样的事情。

谢谢。

1 个答案:

答案 0 :(得分:2)

您正在进行字符串比较。

if(!warns[message.guild.id[rUser.id]]) warns[message.guild.id[rUser.id]] = {
 warns: 0
};

将输出

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

使用整数算术。

true

将不输出任何内容。

相关问题