vijayanand
vijayanand

Reputation: 238

output from ssh stored in a variable to test its value

  remote_file=$(ssh  -t -o LogLevel=QUIET $ip_address 'if [[ -f ~/logs/output.log ]]; then echo exists; else echo nonexistant; fi')
  echo $remote_file
  if [[ "x$remote_file" == "xexists" ]]
  then
     # do some stuff here
  else
     # do some other stuff here
  fi

The variable $remote_file seems to be storing correct value as it prints correct value in the echo statement. But when I test for its value, there seems to be something wrong as the test always fails and goes to else block. I tried to debug using set -x and noticed that the test condition left side is missing. What could be the issue here?

== \x\e\x\i\s\t\s ]]

Upvotes: 0

Views: 41

Answers (1)

G. Sliepen
G. Sliepen

Reputation: 7984

As mentioned by Charles Duffy, the output might contain whitespace characters that are not obvious, but that you need to add to the comparison or remove somehow from the variable $remote_file before doing the comparison.

Note that the example given doesn't really need a string comparison. It might not be obvious, but if you start ssh with a command, then when the command is finished, ssh will exit with the same exit code as the command itself. So you can simplify your code to:

if ssh -t -o LogLevel=QUIET $ip_address 'test -f ~/logs/output.log'
then
    # do some stuff here
else
    # do some other stuff here
fi

Upvotes: 2

Related Questions