Reputation: 15
I'm saving the output of one command executed on a remote via ssh, but I also execute some echos. In order to see the messages on the screen and not save them to the variable I want to redirect them. I have tried >&1 and >&2 but in each case a file is created with the name either "1" or "2" Is this an issue of expanding quotes or escaping characters?
OUTPUT=$(sshpass -p password ssh user@ip 'echo "Message1" >&2 ;
su -lc "./rootscript.sh" >&2;
echo "$?" ')
echo "su output is: $OUTPUT"
Output:
Nothing on screen, a file named "2" with the text "Password" inside (assuming Message 1 was overwritten) The program still waits for a password, even without a prompt, so when I enter it the output is good:
su output is: 0
How can I get my messages to show on screen?
Upvotes: 0
Views: 2220
Reputation: 123410
@JonathanLeffler caught the problem right away -- you're not using bash, but tcsh.
tcsh has its own syntax incompatible with bash.
It doesn't matter though: since ssh
and sshpass
return the remote command's exit code, this is how you should be doing it:
if sshpass -p password ssh user@ip 'echo "Message1"; su -lc "./rootscript.sh"'
then
echo "The command succeeded (exit code $?)"
else
echo "The command failed (exit code $?)"
fi
If you really wanted to run something in bash on the remote shell, you could do use Charles Duffy's code with a minor change:
# v-- shell specified here
output=$(sshpass -p password ssh user@ip bash << 'EOF'
echo "Message1" >&2
su -lc "./rootscript.sh" >&2
echo "$?"
EOF
)
Upvotes: 1