巴什 - 获得输出回路的反向计数 [英] Bash - get a reverse count of loop in output

查看:103
本文介绍了巴什 - 获得输出回路的反向计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望得到循环计数反向输出到知道有多少剩余的循环错过完成。

 #!/斌/庆典
datenow = $(日期+%F)
日志文件=表格 - $ datenow.LOG
RM -f table.sql
MySQL的--login路径= myloginpath -h xhost的-N -e \\
选择一个database.table COUNT(*),其中a.field = 0; |三通-a $日志文件
mysqldump的--login路径= myloginpath小时xhost的数据库表> table.sql
阅读-p插入主机用逗号分隔',':localcs
MySQL的--login路径= myloginpath -h xhost的-N -e \\
从database.hosts n选择n.ipaddress其中n.hosts($ localcs); > ip.txt
在$ IP(猫ip.txt);

MySQL的--login路径= myloginpath -h $ IP数据库< table.sql
回声$ IP |三通-a $日志文件和放大器;&安培; MySQL的--login路径= myloginpath -h $ IP -N -e \\
选择一个database.table COUNT(*),其中a.field = 0; |三通-a $日志文件
DONE

事情是这样的:

  100
(MySQL的输出)
99
(MySQL的输出)
98
(MySQL的输出)
.....


解决方案

您只需要知道有多少行是流提前。

  NUM = $(WC -l< ip.txt)
而阅读-r IP;做
    #做你的工作,使用$ NUM根据需要    #然后再递减NUM
    NUM = $((NUM-1))
完成< ip.txt

顺便说一句,这说明遍历文件的推荐方法;不使用循环。

I want to get a count of loop in reverse in output to know how many loop remaining I miss to finish.

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

Something like this:

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

解决方案

You just need to know how many lines are in the stream in advance.

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

By the way, this shows the recommended way of iterating over a file; don't use a for loop.

这篇关于巴什 - 获得输出回路的反向计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆