bash 使用 ssh 检查远程计算机上是否存在目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34008223/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 13:58:16  来源:igfitidea点击:

Check if directory exists on remote machine with ssh

bashssh

提问by hackchoc

This question has been already posted but I would like to know if there is a way to know if a directory exists on a remote machine with ssh BUT from the command line directly and not from a script. As I saw in this previous post: How to check if dir exists over ssh and return results to host machine, I tried to write in the command line the following:

这个问题已经发布,但我想知道是否有办法直接从命令行而不是从脚本通过 ssh BUT 知道远程计算机上是否存在目录。正如我在上一篇文章中看到的:如何通过 ssh 检查 dir 是否存在并将结果返回到主机,我尝试在命令行中编写以下内容:

ssh [email protected] '[ -d Documents ]'

But this does not print anything. I would like to know if there's a way to display an answer easily.

但这不会打印任何内容。我想知道是否有一种方法可以轻松显示答案。

采纳答案by Jahid

This is a one liner:

这是一个单班轮:

ssh [email protected] '[ -d Documents ] && echo exists || echo does not exist'

回答by Eric Renouf

That command will just return whether or not the Documentsdirectory exists, you can extend the answer in the linked question to do something if it does like:

该命令只会返回Documents目录是否存在,如果确实如此,您可以扩展链接问题中的答案以执行某些操作:

if ssh [email protected] '[ -d Documents ]'; then
    printf "There is a Documents directory\n"
else
    printf "It does not exist, or I failed to check at all\n"
fi

or if you want to store whether or not it exists in a variable you could do something like

或者如果您想存储它是否存在于变量中,您可以执行以下操作

ssh [email protected] '[ -d Documents ]'
is_a_directory=$?

now if is_a_directorycontains 0you know there is a Documentsdirectory, otherwise there is not such a directory or we failed to sshand find out

现在如果is_a_directory包含0你知道有一个Documents目录,否则没有这样的目录或者我们没有ssh找到