ssh -i "path/to/my/key.key" ip.ip.ip.ip "mkdir a/a; sql 'query;' > a/a/file.txt; if [ -f a/a/file.txt ]; then if [ $(wc -l a/a/file.txt) -ge 2 ]; then echo 'done'; fi; fi; exit"
After connecting via ssh, I'm basically making a directory, writing a sql query to a file. If that file exists, then I check if its word count is greater than 1. If word count greater than 1, I echo done, then exit.
However, I get this error:
wc: a/a/file.txt open: No such file or directory
It seems as if the command substitution $(wc -l a/a/file.txt) is executing first (at least before the sql query), so the file hasn't been created yet. Is there some way to force this to execute in the order its written, or is there some better way to do this?
wc -l fileisn't directly usable in a-gttest it has the filename in it. Usewc -l <fileto avoid that.-ftest is not necessary; oncesqlreturns, that file is guaranteed to exist because the shell creates it before it executessql.