[英]Bash Nested for loop keeps starting at line one
I have a script that runs through no problem until it hits the second ($sqlSenderID) and the third($sqlEmail). 我有一个脚本,在命中第二个($ sqlSenderID)和第三个($ sqlEmail)之前,没有问题。
What it does it runs through the $sqlEmail 5 times (as It find five emails) and then it changes the id and do the same thing again and thus returning the wrong info. 它执行的操作是通过$ sqlEmail运行5次(因为它找到五封电子邮件),然后更改id并再次执行相同的操作,从而返回错误的信息。
I would like to have the $sqlEmail to stop and go to $senderID after the first run and then it must run through again with a new ID and email. 我想让$ sqlEmail停止运行,并在第一次运行后转到$ senderID,然后它必须使用新的ID和电子邮件再次运行。
If I add a break in the $sqlEmail loop in goes back but it reports the same email address over and over 如果我在$ sqlEmail循环中添加一个中断,则返回,但是它一遍又一遍地报告相同的电子邮件地址
Any help will be appreciated 任何帮助将不胜感激
my Code: 我的代码:
for i in $sqlSenderID
do
for e in $sqlEmail
do
sqlBannerExp=$(sudo -upostgres psql -d db -t -c "select \"endTime\" from \"lnkSenderTag\" where \"senderId\" = '$i' and \"endTime\" != 'infinity' and \"endTime\" <= 'now'::date;")
if [[ -n $sqlBannerExp ]]; then
echo "$e Banner Expired" >> Banner.txt
fi
sqlBannerSoon=$(sudo -upostgres psql -d db -t -c "select \"endTime\" from \"lnkSenderTag\" where \"senderId\" = '$i' and \"endTime\" != 'infinity' and \"endTime\" = (current_date + interval '1 day');")
if [[ -n $sqlBannerSoon ]]; then
echo "$e Banner Expiring Soon" >> Banner.txt
fi
sqlBannerNo=$(sudo -upostgres psql -d db -t -c "select branded from maillog where sender = '$i' and branded is null;")
if [[ -n $sqlBannerNo ]]; then
echo "$e No Banner Assigned" >> Banner.txt
fi
sqlSignatureNo=$(sudo -upostgres psql -d db -t -c "select tagtype from branding where senderid = '$i' and tagtype != 'Template' and tagtype != 'Disclaimer';")
if [[ -z $sqlSignatureNo ]]; then
echo "$e No Signature Assigned" >> Banner.txt
fi
echo "$e" >> test.txt
break
done
echo "" >> Banner.txt
done
The Sender ID will be something like 451 452 453 845 22472 发件人ID类似于451452453453845472
You want to iterate over two lists in parallel, for which I would use arrays. 您想并行遍历两个列表,为此我将使用数组。
sqlSenderID=(1 2 3)
sqlEmail=(foo@bar baz@qux abc@def)
do_sql () {
sudo -upostgres psql -d db -t -c "$1"
}
for ((j=0; j< ${#sqlSenderID}; j++)); do
i=${sqlSenderID[i]}
e=${sqlEmail[i]}
sqlBannerExp=$(do_sql "select \"endTime\" from \"lnkSenderTag\" where \"senderId\" = '$i' and \"endTime\" != 'infinity' and \"endTime\" <= 'now'::date;")
if [[ -n $sqlBannerExp ]]; then
echo "$e Banner Expired"
fi
sqlBannerSoon=$(do_sql "select \"endTime\" from \"lnkSenderTag\" where \"senderId\" = '$i' and \"endTime\" != 'infinity' and \"endTime\" = (current_date + interval '1 day');")
if [[ -n $sqlBannerSoon ]]; then
echo "$e Banner Expiring Soon"
fi
sqlBannerNo=$(do_sql "select branded from maillog where sender = '$i' and branded is null;")
if [[ -n $sqlBannerNo ]]; then
echo "$e No Banner Assigned"
fi
sqlSignatureNo=$(do_sql "select tagtype from branding where senderid = '$i' and tagtype != 'Template' and tagtype != 'Disclaimer';")
if [[ -z $sqlSignatureNo ]]; then
echo "$e No Signature Assigned"
fi
done >> Banner.txt
Note that unless you have complete control over the contents of the two lists, generating SQL statements dynamically like this is prone to SQL injection attacks. 请注意,除非您完全控制两个列表的内容,否则像这样动态生成SQL语句很容易受到SQL注入攻击。 Consider using a language with a proper SQL library. 考虑使用具有适当SQL库的语言。
I managed to do it a different way that works nicely. 我设法以一种不错的方式进行了处理。
Only thing it creates n blank array item that I don't know how to get rid of it 它唯一会创建n个不知道如何摆脱它的空白数组项
SenderID=()
while read -r output_line; do
SenderID+=("$output_line")
done < <(do_sql "select id from \"mstPerson\" where \"parentAccountId\" = '$f' and email LIKE '%@$d%' and id is not null order by id asc;")
Email=()
while read -r output_line; do
Email+=("$output_line")
done < <(do_sql "select email from \"mstPerson\" where \"parentAccountId\" = '$f' and email LIKE '%@$d%' and email is not null order by id asc;")
echo "${sqlAccountName// }" >> Banner.txt
echo "" >> Banner.txt
done
for i in "${SenderID[@]}"
do
sqlBannerExp=$(do_sql "select \"endTime\" from \"lnkSenderTag\" where \"senderId\" = '$i' and \"endTime\" != 'infinity' and \"endTime\" <= 'now'::date;")
if [[ -n $sqlBannerExp ]]; then
echo "${Email[i]} Banner Expired"
fi
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.