I have a script which uses rsync to sync data in a remote -> local scenario. Immediately after the rsync command is run, a check to see if the error code equals zero or not. If its zero, further commands are performed. This however doesn't take into account the fact that rsync might have ran successfully but not actually made any changes. Because of this the equal zero condition will run regardless, which is a little redundant.
rsync -aEivm --delete /path/to/remote/ /path/to/local/
if [ $? -eq 0 ]; then
# Success do some more work!
else
# Something went wrong!
exit 1;
fi
What would be the best approach to expand this to check if there were actually any changes based on the rsync command that ran. I've read that -i flag can provide output to stdout, but how can this be placed in a conditional block?
-v
in there, so it is already providing the information you need to stdout...e.g., a list of files that were actually sent. If nothing is changed, that's just./
. – goldilocks Apr 25 '15 at 13:35a=$("rsync command")
. This would execute thersync
command and storestdout
ina
. Then you can run tests ona
– nitishch Apr 25 '15 at 18:42| grep /
or something like that, then check the exit status of grep with$?
, it should be 1 if there was no output. – Bjorn Munch Apr 25 '15 at 21:46