2017-07-07 51 views
0

我在bash小片段bash腳本爆發循環不發送郵件

retries = 3 

for ((i=0; i<retries; i++)); do 
    curl -1 --cipher ALL --connect-timeout 90 -T $zip_name ftps://ftp.box.com/Backup/$zip_name --user [email protected]:§fHyFSDF$o6 
    [[ $? -eq 0 ]] && break 
    echo "something went wrong, let's wait 6 seconds and retry" 
    sleep 6 
done 

[[ $retries -eq i ]] && { echo "This email is a notification of Failure" | mail -s "Failed" "[email protected]" ; exit 1; } 

上述循環應重試3次,如果有做捲曲一個錯誤,所以如果捲曲錯誤出現這應該郵寄到電子郵件。但是,爲了測試它我撤銷用戶的訪問,所以我期望的電子郵件,但是這並沒有工作,我收到以下控制檯上:

% Total % Received % Xferd Average Speed Time Time  Time Current 
           Dload Upload Total Spent Left Speed 
    0  0 0  0 0  0  0  0 --:--:-- 0:00:03 --:--:--  0 
curl: (9) Server denied you to change to the given directory 

爲什麼代碼,如果訪問,如果打破了第一次嘗試撤銷上傳服務器

+0

https://www.gnu.org/software/bash/manual/html_node/Quoting.html#Quoting – hek2mgl

+0

@ hek2mgl你可以詳細說明你想說的話? – Kittystone

+0

我說你需要引用(a)變量和(b)包含特殊字符的字符串。我在代碼中看不到引號。 – hek2mgl

回答

0
retries = 3 

for ((i=0; i<$retries; i++)) 
do 
    curl -1 --cipher ALL --connect-timeout 90 -T $zip_name 
ftps://ftp.box.com/Backup/$zip_name --user [email protected]:§fHyFSDF$o6 
    if [[ $? -eq 0 ]] 
    then   
     break 
     echo "something went wrong, let's wait 6 seconds and retry" 
     sleep 6 
    fi 
done 

if [[ $retries -eq $i ]] 
then 
    echo "This email is a notification of Failure" | mail -s "Failed" "[email protected]" 
    exit 1 
fi 

你最好使用if語法我覺得,我注意到你沒有正確引用retries變量。即與$重試,而不是重試和$我而不是我

+0

我試過你的變化以及..錯誤仍然存​​在完全一樣。爲什麼捲曲斷裂而不去? – Kittystone

+0

如果第一次curl命令成功,則循環會發生,但電子郵件不會發送,因爲重試只會是0? –

+0

是的。如果試圖在第四次重試之後嘗試退出,它應該突破併發送電子郵件 – Kittystone