简体   繁体   中英

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).

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.

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.

If I add a break in the $sqlEmail loop in goes back but it reports the same email address over and over

Any help will be appreciated

my Code:

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

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. Consider using a language with a proper SQL library.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM