bash - 带倒计时线的 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}
但没有成功
解决方案
样本输入:
$ 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.
推荐阅读
- python - 获取一个精灵位置,以便下一个精灵可以放在它附近
- python - 如何将多个 tkinter 应用程序导入提供 4 个选项的应用程序?
- python - 如何使用报纸成功抓取网页的标题链接
- javascript - 如何退出我的 switch 语句?
- sql - 如何将数据从 Excel 馈送到 Toad SQL 并创建为视图
- deep-learning - 运行时错误:Cuda 内存不足 - 代码或 gpu 有问题?
- tron - 如何在节点js中使用tronweb无限制地获取tron网络中合约的所有事件日志?
- c# - 静态属性延迟加载正则表达式编译选项
- go - Transform struct into different struct before marshalling to JSON
- ibm-midrange - 在 Rational Developer for I Database 透视图中,更改时间戳格式