1

I have a script that rsyncs and ends like this

rsync -azvh /dir -e ssh usr@server.com:/
RESULT="$?"

# check result of rsync db's
if [ "$RESULT" != "0" ]; then
    echo -e "rsync exit Code:" $RESULT "\nFAILED to rsync backups"
else
    echo "SUCCESSFULL rsync of backups"
fi

I have just been asked to wrap it in an API but the API states that 0=fail and 1=success. How can I change the exit code to reflect this? Do I need to assign it a variable?

  • 5
    “0=fail and 1=success”!! That's very bad. The rest of the world uses 0=success and 1–255=fail for exit statuses. Doing the opposite is incompatible with shell logical operators, with set -e, with make, ... – Gilles 'SO- stop being evil' Nov 4 '16 at 23:15
7

exit 1 will exit with an error code of 1 and exit 0 will exit with an error code of 0.

For instance:

rsync -azvh /dir -e ssh usr@server.com:/
RESULT="$?"

# check result of rsync db's
if [ "$RESULT" != "0" ]; then
    echo -e "rsync exit Code:" $RESULT "\nFAILED to rsync backups"
    exit 0
else
    echo "SUCCESSFULL rsync of backups"
    exit 1
fi
|improve this answer|||||
  • May I ask for an example? – eekfonky Nov 6 '16 at 9:34
  • I added an example. – jayhendren Nov 7 '16 at 19:58
1

Revised answer, report false (exit 1 would work too), and true as needed as per non-standard API:

rsync -azvh /dir -e ssh usr@server.com:/
RESULT="$?"
# check result of rsync db's
if [ "$RESULT" = "0" ]; then
    echo "SUCCESSFULL rsync of backups"
    false
else
    echo -e "rsync exit Code:" $RESULT "\nFAILED to rsync backups"
    true
fi
|improve this answer|||||
  • 1
    Problem there is the exit code 0 relates to it being a success. If I do as you suggest will that not just be a false positive or false negative? – eekfonky Nov 6 '16 at 9:35
  • @eekfonky, thanks very much for that correction -- sorry to say I'd quite misunderstood the question as being about rsync itself returning strange exit codes, rather than the routine's API wrapper. See revised answer above which should be more correct. – agc Nov 6 '16 at 18:50
0

super easy via bash:

test $((`echo "${PIPESTATUS[@]}" | tr ' ' '+'`)) -eq 0
echo $?
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.