2

I want ot check if a path is a file or a directory. I know the path exists. The code below works as long as no spaces are in the path. How to make this work for names with spaces? I currently have a file with a name in it and cannot check if it exists remotely. code reads:

server=<serverip>
path="/var/lib/Fingerprint log.log"

if ssh root@$server test -d $path
then
    echo "Directory"
else
    echo "File"
fi

When I have a path with a space it fails saying

test: /var/lib/Fingerprint: binary operator expected

if path is /var/lib/Fingerprint log.log

fedorqui
  • 275,237
  • 103
  • 548
  • 598
Rafael Urena
  • 111
  • 2
  • 3
  • 11
    Quote your variables. Always quote your variables. – Etan Reisner May 11 '15 at 17:47
  • @EtanReisner what is the SO convention to indicate issue is solved when a comment provides enough hints? – Dinesh May 11 '15 at 17:54
  • @Dinesh: [Question with no answers, but issue solved in the comments](https://meta.stackoverflow.com/questions/251597/question-with-no-answers-but-issue-solved-in-the-comments) – Greg Hewgill May 11 '15 at 17:59
  • @Dinesh Generally I get people telling me to write it as an answer. =) This issue just comes up enough that it didn't feel like it warranted Yet Another answer and I was (admittedly) too lazy to go find a good question to mark this a duplicate of. – Etan Reisner May 11 '15 at 18:00
  • The problem is that there isn't really one obvious duplicate *question*, but rather hundreds of questions that all have the same answer. This is a question that the OP could answer himself by reading the tips and suggestions on the `bash` tag's info page (which, unfortunately, is not easy that easy to find for someone not familiar with Stack Overflow). – chepner May 11 '15 at 18:09
  • Indeed. A high quality generic canonical question would be ideal here but I don't know of one and haven't had time to try to create one. – Etan Reisner May 13 '15 at 15:36

2 Answers2

1

As per the top most comment, you need to use "${path}" instead of $path.

0
path="/var/lib/Fingerprint log.log"
ssh root@$server test -d $path

The short answer is that you should run ssh like this:

ssh root@$server test -d "'$path'"
or
ssh root@$server "test -d '$path'"

The double quotes ensure that the $path variable is expanded on the local system, and also that the single quotes are passed through to the remote system. The single quotes ensure that the value of $path is still treated as a single command-line argument on the remote system.

This is due to how ssh handles remote commands that are specified on its command line. First, it concatenates all of the relevant command-line arguments into a single space-separated string. Then it sends that string to the remote system, where it's run as a shell command.

For example:

$ dne='/var/does not exist'
$ ssh localhost ls $dne
ls: cannot access /var/does: No such file or directory
ls: cannot access not: No such file or directory
ls: cannot access exist: No such file or directory

$ ssh localhost ls "$dne"
ls: cannot access /var/does: No such file or directory
ls: cannot access not: No such file or directory
ls: cannot access exist: No such file or directory

$ ssh localhost ls "'$dne'"
ls: cannot access /var/does not exist: No such file or directory
Kenster
  • 23,465
  • 21
  • 80
  • 106