I am running a chain of sshpass > ssh > commands, I would like to store the exit code of one of those commands ran in the remote server in a variable so I can access it back in the local after ssh is done. What I have done so far does not seem to store anything. Help please!
My Code:
sshpass -p password ssh user@ip "echo \"first command\" ; su -lc \"./root_script.sh\" ; set MYVAR = $? ; rm root_script.sh \"
if (( $MYVAR != 0 )) ; then
echo "Cannot continue program without root password"
exit
fi
Problem: The commands are all executed (the script runs ok) but the variable MYVAR is not set. I have initialized this to a weird number, and the value does not change. The su exit code is not stored!
Notes:
1) I don't want to do MYVAR=$(ssh....) since ssh is nested within a sshpass, and I don't want the exit code of either of those, I want the return code of the su command ran in the remote server
2) I have used set command because simple assignment gives me an error saying command is not recognized.
3) I have tried different forms of quoting ( \$? or '$?' or "$?" ) but none seem to work
4) I have tried exporting MYVAR to an environment variable, and I have tried unsetting it prior to the line of code. Still no value is stored.
myvar=$(ssh ...)doesn't store the exit code, but rather stores the output. Unless sshpass modifies output, it shouldn't be relevant here.exportmakes a variable accessible to subprocesses. Nothing you can do makes a variable accessible to parent processes without the parent's active involvement (such as reading and storing or eval'ing its child's output stream).echo $?to return the exit status of the su command.