I have a script in which I'm meticulously checking return codes for error conditions, so that I can abort early in the event of a failure. One step of this script involves running a command as the root user on another box, via ssh and sudo.
Consider:
ssh $HOST sudo $CMD
echo $?
ssh passes return codes back just fine, but even if $CMD returns a nonzero exit code, sudo still returns 0 after running the command.
How do I capture the return code to $CMD? I'm very partial to its being passed back as ssh's return code, but if there's another simple method which can't be confused by the output of $CMD, I'm all ears.