1

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.

3
  • 1
    myvar=$(ssh ...) doesn't store the exit code, but rather stores the output. Unless sshpass modifies output, it shouldn't be relevant here. Commented Jul 28, 2014 at 20:14
  • 3
    ...beyond that -- export makes 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). Commented Jul 28, 2014 at 20:15
  • Hence, you could echo $? to return the exit status of the su command. Commented Jul 28, 2014 at 20:15

1 Answer 1

2

First, you need to put the content you want to capture on stdout of the process.

Second, you need to use single-quotes, not double-quotes, for the remote command -- otherwise, $? will be replaced with its local value before the string is ever passed to ssh!

exit_status=$(sshpass -p password ssh user@ip 'echo "first command" >&2; \
              su -lc "./root_script.sh" >&2; echo "$?"; rm root_script.sh')
echo "Exit status of remote command is $exit_status"
Sign up to request clarification or add additional context in comments.

5 Comments

There is already an echo as a command, giving the user status information. Therefore the first echo will be stored as well into exit_status, wont it?
@natharra, indeed; I was assuming you were going to replace it with a real command, but if you actually want to write messages to the user, use >&2 to direct them to stderr.
Ah, of course. I have to play around a bit with the re-directions since there are some messages I do want the user to see ("password:" and some in the script) but I understand the concept. Thanks!
Password prompts are often sent directly to the TTY rather than going through stdout/stderr, but... well, that's a bigger topic.
it is indeed a bigger topic @CharlesDuffy. I am still having some trouble redirecting things correctly. However, it is a whole new question, mind looking into it?: stackoverflow.com/questions/25018675/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.