Accessing an exit code outside of a su -m $USER -c "<cmd>"

The below command prints "0" or "1" correctly depending on failure or not because i put $? inside the "su" command.

sudo su -m $USER -c "./shutdown.sh &> /dev/null; echo \$?"

However, if i do this:

sudo su -m $USER -c "$BASE/bin/shutdown.sh &> /dev/null"; echo $?

it will always print "0" because the actual "su" will always succeed, even if the command portion fails.

Any idea how I can retrieve the exit code of the subshell?


Solution 1:

I just fixed almost exactly the same situation. Hope it still helps you, if not then perhaps others. I started from su, not sudo, but since sudo is intended to wrap a single other command, it really ought to relay su's exit code. If not, you can apply the fix below on the sudo level also.

Like you noted the main problem is that su succesfully executes it's command. The default action is then to report that it completed without any problems and so it returns exit code 0. It doesn't "know" that a non 0 exit code from a command was unexpected, or that it should do something with it. Thus the solution is quite simply to make su return the exit code of it's last command. This did it for me

su <user_x> -c '<bunch_of_commands>; exit $?'

In case sudo doesn't play nice, the whole command should be something like this (I would check this for you but I don't have sudo installed)

sudo 'su <user_x> -c \'<bunch_of_commands>; exit $?\'; exit$?'

Watch for the nesting of quotes and make sure $? doesn't get expanded, so no double quotes.

Solution 2:

Echo the return value inside the command and access it outside by assigning the whole command to a variable (using a subshell.)

RETVAL=$(sudo su -m $USER -c "./shutdown.sh &> /dev/null; echo \$?")
echo $RETVAL