31

I'm trying to establish an SSH connection and see if a directory exist, and if that directory exists I want to run commands on the local machine that made the SSH call.

Here is what I've attempted:

if [ ssh -t username@ssh_server -d /directory ]
then
{
    commands....
}
fi

Is something like this possible?

codeforester
  • 39,467
  • 16
  • 112
  • 140
nullByteMe
  • 6,141
  • 13
  • 62
  • 99
  • Yes it is. However, it is better to get data firstly, store in a variable and play with it in your `if`. – fedorqui Apr 10 '13 at 13:57
  • How do get data via ssh on a server and send it back to my local machine? I know how to do it the other way around.. – nullByteMe Apr 10 '13 at 13:59
  • 2
    Could I suggest that this question is "unmarked" as duplicate? This particular question (and it's answer) has a use case similar to mine involving testing remote file existence using SSH and thanks to the answer, I got a problem solved. The other question being referred to (that is considered already answering this question) have no references to SSH or running tests and commands on a remote machine so I really consider both questions a bit distinct. (I upvoted that other question as well if anyone wonders) – IllvilJa Jan 03 '18 at 13:19
  • I agree. This question is definitely not a duplicate. This question does involve bash in some degree as the person is setting up an example but the question is about returning the result to an application on another computer that may or may not be running Linux or similar. It is nothing about Bash syntax specifically but simply returning a usable response.The title says it all. – Richard Robertson May 01 '18 at 19:51
  • @tripleee This post was marked as duplicate. The supposed "duplicate" answer addresses checking existence of a directory on a local host. This question addresses checking existence of a directory on a remote host. – MrMas Nov 06 '18 at 19:12
  • 1
    @MrMas The [guidance for duplicates](https://meta.stackexchange.com/questions/194476/someone-flagged-my-question-as-already-answered-but-its-not/194495#194495) is that identical *answers* is grounds for marking as duplicate. In the case of a syntax error like this, the answer explains what the syntax error is, not so much how to accomplish the specific end goal in this specific question. – tripleee Nov 07 '18 at 05:52
  • 1
    For what it's worth, I added a second duplicate to an answer which duplicates the specific answer here. – tripleee Nov 07 '18 at 05:59
  • @tripleee It seems like there should be a way to distinguish the nuance you're describing. I have found questions that are "closed as duplicates" when in fact I need an answer to that question not the answers that relate to that question and I have been frustrated by that fact. – MrMas Dec 08 '20 at 18:33
  • 1
    @MrMas That's another reason why we request every question to be a [mre]. When the question focuses on exactly one, clearly described issue, this kind of frustration can be avoided, and the value for future visitors is much improved. – tripleee Dec 09 '20 at 05:55

1 Answers1

63

You are very close:

Change if statement to

if ssh username@ssh_server '[ -d /directory ]'

I am assuming that you have setup key-based authentication.

anishsane
  • 20,270
  • 5
  • 40
  • 73