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

标签 mysql linux bash shell

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

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

相关文章:

linux - 如何在 bash 中使用 grep 测试进程是否正在运行?

java - 多线程——MySQL java connector prepared statement使用

c - 使用 mempcpy 在 for 循环中串行构造字符串会导致无限递归

MySQL CROSSTAB 或 PIVOT 查询

linux - 使用来自 NoMachine 的 'NX free edition for Linux' 在 Amazon EC2 上运行 Ubuntu 13.04/Linux mint 14

linux - 使用 linux 命令 "date"添加一些特定时间

linux - 创建 Bash 脚本来检查和禁用 SELinux : How to Prompt User Input?

linux - rpm --test 从不返回非空字符串?

mysql - Google Cloud MySQL 基于行的复制数据类型错误

php - 如何统计变量值是否重复?