/ / Bash Zagnieżdżona pętla for zaczyna się od pierwszej linii - bash, for-loop

Bash Zagnieżdżona pętla for zaczyna się od pierwszej linii - bash, for-loop

Mam skrypt, który działa bez problemu, dopóki nie trafi w drugi ($ sqlSenderID) i trzeci ($ sqlEmail).

To, co robi, działa przez $ 5 sqlEmail (ponieważ znajduje pięć e-maili), a następnie zmienia identyfikator i robi to samo ponownie, zwracając w ten sposób błędne informacje.

Chciałbym, aby $ sqlEmail zatrzymał się i udał się do $ senderID po pierwszym uruchomieniu, a następnie musi ponownie uruchomić z nowym identyfikatorem i e-mailem.

Jeśli dodam przerwę w pętli $ sqlEmail, to wraca, ale w kółko zgłasza ten sam adres e-mail

Każda pomoc zostanie doceniona

mój kod:

kod

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

Identyfikator nadawcy będzie miał wartość 451 452 453 845 22472

Odpowiedzi:

0 dla odpowiedzi № 1

Chcesz iterować równolegle dwie listy, dla których używałbym tablic.

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

Pamiętaj, że jeśli nie masz kompletny kontrola zawartości dwóch list, dynamiczne generowanie instrukcji SQL jest podatne na ataki SQL injection. Rozważ użycie języka z odpowiednią biblioteką SQL.


0 dla odpowiedzi nr 2

Udało mi się zrobić to w inny sposób, który działa dobrze.

Jedyną rzeczą, która tworzy n pusty element tablicy, którego nie wiem, jak się go pozbyć

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