首页 > 解决方案 > 带倒计时线的 while 循环

问题描述

我在文本文件中有一个网址列表,如下所示

https://example.com/test.php?x=1
https://example.com/test.php?x=1&y=2

我正在执行while循环,同时在这些行上执行某些命令,如下所示

while read line ;
        do command ;
done < list.txt

我需要在终端上打印循环函数中的计数器,这样我就可以知道到列表末尾的估计时间或另一个词来知道到列表末尾的其余行。

我试过awk -v x="$url" '$0~x {print NR}但没有成功

标签: bashawk

解决方案


样本输入:

$ cat list.txt
https://example.com/test.php?x=1
https://example.com/test.php?x=1&y=2
https://example.com/test.php?x=1&y=3
https://example.com/test.php?x=1&y=4
https://example.com/test.php?x=1&y=5
https://example.com/test.php?x=1&y=6
https://example.com/test.php?x=1&y=7

一个想法:

urlcount=$(wc -l < list.txt)
loopcount=0

while read -r url
do
    ((loopcount++))
    echo "Processing URL #${loopcount} (of ${urlcount}) [ ${url} ] ..."

    sleep 3     # replace with 'command ...'

done < list.txt

echo "Done."

生成以下输出(以 3 秒间隔打印的行):

Processing URL #1 (of 7) [ https://example.com/test.php?x=1 ] ...
Processing URL #2 (of 7) [ https://example.com/test.php?x=1&y=2 ] ...
Processing URL #3 (of 7) [ https://example.com/test.php?x=1&y=3 ] ...
Processing URL #4 (of 7) [ https://example.com/test.php?x=1&y=4 ] ...
Processing URL #5 (of 7) [ https://example.com/test.php?x=1&y=5 ] ...
Processing URL #6 (of 7) [ https://example.com/test.php?x=1&y=6 ] ...
Processing URL #7 (of 7) [ https://example.com/test.php?x=1&y=7 ] ...
Done.

推荐阅读