Two things you may want to look up:
1. 'if' can be used to execute blocks of code depending upon the code return from a command, ie
Code:
if rsync command
then
dostuff
else
do other stuff
fi
works.
2. the environment is loaded with a value of the code return from the last run command. You can use this to test and run conditional code after the command exits. You will find this documented in your shell man pages.
You should be able to google for bash tricks and find both of these mentioned on the internet.