Bash Nested for循环始于第一行

时间:2017-06-01 10:09:04

标签: bash for-loop

我有一个脚本在遇到第二个($ sqlSenderID)和第三个($ sqlEmail)之前没有问题。

它通过$ sqlEmail运行5次(因为它找到了5封电子邮件),然后它更改了id并再次执行相同的操作,从而返回错误的信息。

我希望$ sqlEmail在第一次运行后停止并转到$ senderID,然后必须使用新ID和电子邮件再次运行。

如果我在$ sqlEmail循环中添加一个中断,但是它会一遍又一遍地报告相同的电子邮件地址

任何帮助将不胜感激

我的代码:

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

发件人ID将类似于451 452 453 845 22472

2 个答案:

答案 0 :(得分:0)

你想要并行迭代两个列表,我会使用数组。

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库的语言。

答案 1 :(得分:0)

我设法用不同的方式做到了。

它只创建了一个我不知道如何摆脱它的空白数组项目

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