How to check if dir exists over ssh and return res

2019-01-24 01:38发布

问题:

This question already has an answer here:

  • Checking the success of a command in a bash `if [ .. ]` statement 2 answers
  • Check if directory exists on remote machine with ssh 2 answers

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?

回答1:

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.



标签: bash shell ssh