0

Idea is to check remote folder. Is it exist or no? I try this

ssh -p 22 -tt user@server.com 'bash -d /home/test22

and it works fine

/home/test22: /home/test22: is a directory Connection to server.com closed.

But when I try to use it with "IF" - it is wrong...always said "That directory exists"

#!/bin/bash

if [ "ssh -p 22 -tt user@server.com  'bash -d /home/test22'" ]; then
    echo "That directory exists"
else
    echo "That directory doesn't exists"
fi

Can you show correct example?

Thank you!

Artem Ganzha
  • 11
  • 1
  • 4
  • 1
    That reply looks like an error message, not like a "result". Such error is written to `stdout`, the error output. I do nto see how you want to use that in a conditional. You might want to take a look at the `file` command. There is a `man page` for the command, as always. – arkascha Nov 07 '15 at 13:38
  • I just want to check remote directory and run depended condition. – Artem Ganzha Nov 07 '15 at 13:50
  • 3
    Possible duplicate of [How to check if dir exist over ssh and return results to host machine](http://stackoverflow.com/questions/15927911/how-to-check-if-dir-exist-over-ssh-and-return-results-to-host-machine) – jayant Nov 07 '15 at 13:53
  • #!/bin/bash if [ ssh -p 22 -tt user@com '[ -d /home/test22]' ]; then echo "That directory exists" else echo "That directory doesn't exists" fi ----------------- like answer : -bash: [: too many arguments – Artem Ganzha Nov 07 '15 at 14:03

1 Answers1

4

The command to use is not bash but test or its alias [:

if ssh -p 22 -tt user@server.com  'test -d /home/test22'; then
    echo "That directory exists"
else
    echo "That directory doesn't exists"
fi

The other important thing is that [] is not part of the if syntax. if is always followed by a command and if evaluates its exit status. So no need to surround your ssh command with [].

-d dir are actually the arguments of command test or [.

test -d dir
[ -d dir ]

are identical, the latter form requires a closing ] argument.

SzG
  • 12,333
  • 4
  • 28
  • 41