I wish to use shell to invoke a script on a remote server.
I would like to capture the output of that script (its logging messages) and the exit code it returns.
If I do this:
ssh user@server /usr/local/scripts/test_ping.sh
echo "$?"
I get the exit code but can't capture the remote logging messages .
If I do this:
local RESULTS=$(ssh user@server /usr/local/scripts/test_ping.sh)
echo "$?"
LOG "${RESULTS}";
I get to log my output using my LOG function but can't seem to get a correct exit code, I assume the code I get is the code from the varianble assignment.
I would like to continue to use my LOG function to capture all output as it formats and sends things to a file, syslog, and the screen for me.
How can I capture results in a var AND get the correct exit code from the remote script?
Best Answer
The reason you are not getting the correct error code is because
local
is actually the last thing executed. You need to declare the variable as local prior to running the command.You can see the issue here: