bash 通过 ssh 传递 find 命令的问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23206307/
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 10:16:20  来源:igfitidea点击:

Issue with passing find command via ssh

linuxbashunixsshsolaris

提问by user1028948

I am trying to read content on specific files. I have multiple hosts where these files are located. I am passing the commands below during ssh so that it can run and get the output.

我正在尝试阅读特定文件的内容。我有多个主机,这些文件所在的位置。我在 ssh 期间传递以下命令,以便它可以运行并获取输出。

  1. When using the command below I get the specified errors. (its sshed to host but nothing happens)

    $ ssh host "more $(find /my/path -name "test*")"
    find: 0652-010 The starting directory is not valid.
    
  2. When using exec its not working, (its sshed to host but nothing happens)

    $ ssh host "find /my/path -name "test*" -exec more {} \;"`
    
  3. When using xargs it is working as expected

    $ ssh host "find /my/path -name "test*" | xargs more"
    
  1. 使用下面的命令时,我收到指定的错误。(它 sshed 到主机,但没有任何反应)

    $ ssh host "more $(find /my/path -name "test*")"
    find: 0652-010 The starting directory is not valid.
    
  2. 使用 exec 时它不起作用,(它 sshed 到主机但没有任何反应)

    $ ssh host "find /my/path -name "test*" -exec more {} \;"`
    
  3. 使用 xargs 时,它按预期工作

    $ ssh host "find /my/path -name "test*" | xargs more"
    

Can someone explain why method 1 & 2 are not working?

有人可以解释为什么方法 1 和 2 不起作用吗?

Please note that if I directly run command on remote host every method is working. for example all commands below are working as expected in remote host.

请注意,如果我直接在远程主机上运行命令,则每种方法都有效。例如,下面的所有命令在远程主机上都按预期工作。

more $(find /my/path -name "test*")

find /my/path -name "test*" -exec more {} \;

find /my/path -name "test*" | xargs more

回答by DouglasDD

For (1) the "... $(command substitution)"is being done by your localbash shell, you'll need to escape it so that that the "...$()"literal double quoted string isn't present until the command reaches the remote box:

对于 (1)"... $(command substitution)"本地bash shell完成的操作,您需要对其进行转义,以便"...$()"在命令到达远程框之前不存在文字双引号字符串:

ssh ddd@$t "more $(find ds -name README)"

OR

或者

ssh ddd@$t 'more $(find ds -name README)'

For (2) similarly, you need to escape the \so it is passed correcltly to the remote box:

对于(2)同样,您需要转义,\以便正确地将其传递给远程框:

ssh ddd@$t "find ds -name README -exec more {} \;"

OR

或者

ssh ddd@$t 'find ds -name README -exec more {} \;'

回答by arkascha

Your syntax is wrong. Especially take a look at the double quotes: you enclose the set of double quotes around the test*insideanother set of double quotes. How should a shell understand that? Instead you have to "escape" (so protect) the inner quotes. And you should use the more(or less) command locallysince you want to limit to the local terminal length:

你的语法是错误的。特别是看看双引号:您将一组双引号括test*另一组双引号内。壳应该如何理解?相反,您必须“转义”(因此保护)内部引号。您应该在本地使用more(or less) 命令因为您想限制本地终端长度:

This version of your first attempt works as expected:

您第一次尝试的这个版本按预期工作:

ssh host "find /my/path -name \"test*\"" | more

回答by user3159253

The answer is quoting.

答案是引用。

In the first case you run findlocally. Try ssh host "echo $(hostname)". The second method runs moreon suitable each fileseparately.

在第一种情况下,您在find本地运行。试试ssh host "echo $(hostname)"。第二种方法分别more在合适的每个文件上运行。

回答by Niroj Pattnaik

You should do as below:

你应该做如下:

ssh host "ksh -c 'find /my/path -name \"test*\" -exec more {} \;'"`

ssh 主机 "ksh -c 'find /my/path -name \"test*\" -exec more {} \;'"`

When use "-c" as an option ksh reads the command as a string and run it directly

使用“-c”作为选项时,ksh 将命令作为字符串读取并直接运行