Capturing exit code from sudo-run process

I have a script in which I carefully check the return codes for error conditions, so that I can interrupt myself at the beginning of the failure. One step of this script involves running the command as root in another field via ssh and sudo.

Consider:

ssh $HOST sudo $CMD
echo $?

ssh returns the return codes back only fine, but even if $ CMD returns a non-zero exit code, sudo still returns 0 after running the command.

How do I return a return code in $ CMD? I am very partial to being passed back as an ssh return code, but if there is another simple method that should not be confused with the output of $ CMD, I’m all ears.

+5
source share
1 answer

, sudo . , , $? . $? , .

+4

All Articles