2012-10-19 43 views
0

我有一個簡單的腳本來從遠程服務器獲取數據的過程中它使用rsync產生:有沒有一種簡單的方法來測試bash中一組命令的退出狀態?

while : 
do 
    rsync -avz --remove-source-files -e ssh [email protected]:path/to/foo* ./ 

    rsync -avz --remove-source-files -e ssh [email protected]:path/to/bar* ./ 

    rsync -avz --remove-source-files -e ssh [email protected]:path/to/baz* ./ 

    rsync -avz --remove-source-files -e ssh [email protected]:path/to/qux* ./ 

    sleep 900 #wait 15 minutes, try again 
done 

如果沒有文件,rsync返回退出狀態12(顯然)。如果上面的調用rsync找到任何數據,我想打破循環(生成數據的過程可能已退出)。爲了減輕任何困惑,我做了而不是想要從循環中突破,如果即使是1的rsync進程成功。

在bash中有一個簡潔的方法嗎?

+0

退出狀態12似乎與協議數據流中的錯誤有關。你想退出任何*非零退出狀態(來自所有'rsync'進程)的循環嗎? – ezod

+0

@ezod - 我認爲任何非零退出狀態都足夠讓我打破(但只有當它們都不爲零時)。 – mgilson

回答

0

這種方式計算由於沒有文件而失敗的次數。

while : 
do 
    nofile=0 

    rsync -avz --remove-source-files -e ssh [email protected]:path/to/foo* ./ 
    (($? == 12)) && let nofile++ 

    rsync -avz --remove-source-files -e ssh [email protected]:path/to/bar* ./ 
    (($? == 12)) && let nofile++ 

    rsync -avz --remove-source-files -e ssh [email protected]:path/to/baz* ./ 
    (($? == 12)) && let nofile++ 

    rsync -avz --remove-source-files -e ssh [email protected]:path/to/qux* ./ 
    (($? == 12)) && let nofile++ 

    # if all failed due to "no files", break the loop 
    if (($nofile == 4)); then break; fi 

    sleep 900 #wait 15 minutes, try again 
done 
2

您可以通過添加了返回值,所以,如果他們都返回12,總和是48做到這一點:

while : 
do 
    rc=0 
    rsync -avz --remove-source-files -e ssh [email protected]:path/to/foo* ./ 
    let rc+=$? 

    rsync -avz --remove-source-files -e ssh [email protected]:path/to/bar* ./ 
    let rc+=$? 

    rsync -avz --remove-source-files -e ssh [email protected]:path/to/baz* ./ 
    let rc+=$? 

    rsync -avz --remove-source-files -e ssh [email protected]:path/to/qux* ./ 
    let rc+=$? 

    if [[ $rc == 48 ]]; then # 48 = 4 * 12 
     break; 
    fi 

    sleep 900 #wait 15 minutes, try again 
done 

請注意,如果你得到的返回碼和的另一種組合可能遭受48,即0 + 0 + 12 + 36

+0

我想過這樣的事情。我不想依靠它實際上使用退出狀態12,雖然...這是更多布爾類型的事情。我想我可以做一些像rsync ... ||讓rc + = 1'然後'if [[$ rc -eq 4]];然後打破; fi' ... – mgilson

+0

好主意,但爲了避免擔心其他組合,爲什麼不把它們相互追加$ rc值,然後在* [1-9] *中使用'case $ rc *)print「found non -zero退出代碼= $ rc,無法繼續「; 1號出口;; esac'。祝你們好運。 – shellter

+0

只需將它們相加並打破總和不爲零的情況。 – chepner

0

由其他答案的啓發,我覺得這是我能做到這一點迄今最清晰的方式...

while : 
do 
    do_continue=0 

    rsync -avz --remove-source-files -e ssh [email protected]:path/to/foo* ./ && do_continue=1 
    rsync -avz --remove-source-files -e ssh [email protected]:path/to/bar* ./ && do_continue=1 
    rsync -avz --remove-source-files -e ssh [email protected]:path/to/baz* ./ && do_continue=1 
    rsync -avz --remove-source-files -e ssh [email protected]:path/to/qux* ./ && do_continue=1 

    if [[ $do_continue == 0 ]]; then 
     break 
    fi 

    sleep 900 #wait 15 minutes, try again 
done 

這可能進行重構,以去除break語句和相關條件測試:

do_continue=1 
while [ do_continue -eq 1 ]; do 
    do_continue=0 
    rsync -avz --remove-source-files -e ssh [email protected]:path/to/foo* ./ && do_continue=1 
    #... 
    sleep 900 
done 
相關問題