Search code examples
bashrsyncexitstatus

Is there a succint way to test exit status of a set of commands in bash?


I have a simple script to pull data from a remote server as a process generates it using rsync:

while :
do
    rsync -avz --remove-source-files -e ssh me@remote:path/to/foo* ./

    rsync -avz --remove-source-files -e ssh me@remote:path/to/bar* ./

    rsync -avz --remove-source-files -e ssh me@remote:path/to/baz* ./

    rsync -avz --remove-source-files -e ssh me@remote:path/to/qux* ./

    sleep 900 #wait 15 minutes, try again
done

If there are no files, rsync returns exit status 12 (apparently). In the event that none of the above calls to rsync finds any data, I would like to break from the loop (the process generating the data probably exited). To alleviate any confusion, I do not want to break from the loop if even 1 of the rsync processes succeed.

Is there a succinct way to do that in bash?


Solution

  • This way counts the number of fails due to no file.

    while :
    do
        nofile=0
    
        rsync -avz --remove-source-files -e ssh me@remote:path/to/foo* ./
        (( $? == 12 )) && let nofile++
    
        rsync -avz --remove-source-files -e ssh me@remote:path/to/bar* ./
        (( $? == 12 )) && let nofile++
    
        rsync -avz --remove-source-files -e ssh me@remote:path/to/baz* ./
        (( $? == 12 )) && let nofile++
    
        rsync -avz --remove-source-files -e ssh me@remote: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