Palace Chan
Palace Chan

Reputation: 9213

How can I get the return code for a command which ran via ssh and not the ssh command itself?

I am trying to write a bash script in which one of the first steps is to check for the presence of a file in a remote host and whether it is executable or not. This sounds like a job for the test command, but I'd have to prefix it with ssh. Something like this:

$(ssh user@box 'test -x /path/thefile >/dev/null 2>&1; $?')

Except the above doesn't seem to be working to return to me whether the remote file exists and is executable...how would I do this?

Upvotes: 1

Views: 483

Answers (1)

Vaughn Cato
Vaughn Cato

Reputation: 64308

When you use the $() syntax, you are getting the output of the command, and not the result code. You can just do:

if ssh user@box 'test -x /path/thefile'; then
  echo "It exists"
fi

Upvotes: 2

Related Questions