/ / Bash Nested for loop continue à partir de la ligne un - bash, for-loop

Bash Nested for loop continue à partir de la ligne un - bash, for-loop

J'ai un script qui ne traverse aucun problème jusqu'à ce qu'il touche le deuxième ($ sqlSenderID) et le troisième ($ sqlEmail).

Ce qu'il fait, il passe 5 fois par $ sqlEmail (car il trouve cinq e-mails), puis il change l'identifiant et fait à nouveau la même chose et renvoie ainsi les mauvaises informations.

Je voudrais que $ sqlEmail s'arrête et passe à $ senderID après la première exécution, puis il doit recommencer avec un nouvel ID et un nouvel e-mail.

Si j'ajoute une pause dans la boucle $ sqlEmail, elle revient mais elle rapporte la même adresse e-mail encore et encore

toute aide serait appréciée

mon 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

L'ID de l'expéditeur sera quelque chose comme 451 452 453 845 22472

Réponses:

0 pour la réponse № 1

Vous voulez parcourir deux listes en parallèle, pour lesquelles j'utiliserais des tableaux.

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

Notez que sauf si vous avez Achevée le contrôle du contenu des deux listes, la génération dynamique d'instructions SQL comme celle-ci est sujette aux attaques par injection SQL. Envisagez d'utiliser un langage avec une bibliothèque SQL appropriée.


0 pour la réponse № 2

J'ai réussi à le faire d'une manière différente qui fonctionne bien.

La seule chose qu'il crée n élément de tableau vide que je ne sais pas comment m'en débarrasser

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