2017-06-01 74 views
0

我有一個腳本直到遇到第二個($ sqlSenderID)和第三個($ sqlEmail)時纔會遇到問題。Bash嵌套for循環始終在第一行開始

它做了什麼它通過$ 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

+1

請您可以修復您的格式?我猜想整件事不應該在反引號內,而且'>'也不應該在那裏。 –

+0

我已經刪除反引號,對不起,這是偶然添加它的網站 – Dreamsword981

+1

好吧,現在看起來好多了,但仍有很多代碼需要挖掘。你可以嘗試給我們提供[mcve]嗎? –

回答

0

你想並行迭代兩個列表,爲此我將使用數組。

sqlSenderID=(1 2 3) 
sqlEmail=([email protected] [email protected] [email protected]) 

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庫的語言。

+0

謝謝你,但不幸的是發件人ID不遵循訂購。但是代碼失敗。 – Dreamsword981

0

我設法做了一個很好的工作方式。

它創建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