gpt4 book ai didi

mysql - Bash - 在输出中获取循环的反向计数

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:57:35 24 4
gpt4 key购买 nike

我想在输出中获得反向循环的计数,以了解我还没有完成多少个循环。

#!/bin/bash
datenow=$(date +"%F")
logfile="table-"$datenow".log"
rm -f table.sql
mysql --login-path=myloginpath -h xhost -N -e \
"select count(*) from database.table a where a.field = 0;" | tee -a $logfile
mysqldump --login-path=myloginpath-h xhost database table > table.sql
read -p "Insert Host Separated by comma ',' : " localcs
mysql --login-path=myloginpath -h xhost -N -e \
"select n.ipaddress from database.hosts n where n.hosts in ($localcs);" > ip.txt
for ip in $(cat ip.txt);
do
mysql --login-path=myloginpath -h $ip "database" < "table.sql"
echo $ip | tee -a $logfile && mysql --login-path=myloginpath -h $ip -N -e \
"select count(*) from database.table a where a.field = 0;" | tee -a $logfile
done

像这样:

100
(mysql output)
99
(mysql output)
98
(mysql output)
.....

最佳答案

你只需要提前知道流中有多少行。

num=$(wc -l < ip.txt)
while read -r ip; do
# Do your work, use $num as needed

# Then decrement num
num=$((num-1))
done < ip.txt

顺便说一句,这显示了迭代文件的推荐方式;不要使用 for 循环。

关于mysql - Bash - 在输出中获取循环的反向计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29801249/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com