[英]Bash Nested for loop keeps starting at line one
我有一个脚本,在命中第二个($ sqlSenderID)和第三个($ sqlEmail)之前,没有问题。
它执行的操作是通过$ sqlEmail运行5次(因为它找到五封电子邮件),然后更改id并再次执行相同的操作,从而返回错误的信息。
我想让$ sqlEmail停止运行,并在第一次运行后转到$ senderID,然后它必须使用新的ID和电子邮件再次运行。
如果我在$ sqlEmail循环中添加一个中断,则返回,但是它一遍又一遍地报告相同的电子邮件地址
任何帮助将不胜感激
我的代码:
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
发件人ID类似于451452453453845472
您想并行遍历两个列表,为此我将使用数组。
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
请注意,除非您完全控制两个列表的内容,否则像这样动态生成SQL语句很容易受到SQL注入攻击。 考虑使用具有适当SQL库的语言。
我设法以一种不错的方式进行了处理。
它唯一会创建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.