2015-02-11 222 views
1

我是shell脚本新手,我试图从shell脚本运行以下命令。从shell脚本运行ssh命令

diff <(ssh [email protected]_host 'cat remote_file.txt') <(ssh [email protected]_host2 'cat remote_file2.txt') 

,但得到的错误:

./a.sh: syntax error at line 1: `(' unexpected 

我尝试了一些例子槽googleing它说,使用$()周围的代码,但它没有工作,任何一个可以请帮我这个。

+0

听起来你正在使用'/斌/ sh'您的脚本不支持进程替换。尝试使用'/ bin/bash'。 – 2015-02-11 12:47:15

+0

嗨,Etan,我试过'#!/ bin.sh'和'#!/ bin/bash'这两种情况下,我得到一个错误 './a.sh:语法错误在第2行:'vars = $ 'unexpected'' – 2015-02-11 12:56:55

+0

你是如何运行脚本的?它有执行权限吗?该片段只有一行。如果你从第2行中得到错误,我们不可能帮助你(第2行的错误意味着'bash'接受了<()'语法)。 – 2015-02-11 15:44:57

回答

2

我知道你想在'diff'中使用两个远程文件的输出。有很多事情错在你的解决方案:

  • 不能重定向两个输出流成一个输入流
  • 差异只适用于文件,而不是标准输入
  • 使用来自一个命令的输出作为输入另一个命令是使用管道完成的。重定向是从文件到命令或从命令到文件。
  • 圆括号用于捆绑命令,例如“(echo a; echo b)| cat”将导致'echo a'和'echo'b'的输出在一个流中传递给'cat ',它会打印出来。

我会做什么(完成什么我想你想要做的),是把它分为三个独立的命令:

ssh [email protected]_host 'cat remote_file.txt' > file1 
ssh [email protected]_host2 'cat remote_file.txt' > file2 
diff file1 file2 
+0

这工作,感谢亨克 – 2015-02-11 13:09:08

+0

大家好,我 创建了以下想法shell脚本所呈现,主要内容如下:

 ' #!/usr/local/bin/bash srchost='echo $1 | cut -d ':' -f 1' srcpath='echo $1 | cut -d ':' -f 2' dishost='echo $2 | cut -d ':' -f 1' dispath='echo $2 | cut -d ':' -f 2' echo $srchost echo $srcpath echo $dispath echo $dishost ssh $srchost 'cat $srcpath' > file1 ssh $dishost 'cat $dispath' > file2 diff file1 file2 ' 
**此脚本陷入** – 2015-02-11 13:32:22

+0

'<()'是不是输入重定向。它是进程替代,并且在单个命令行上使用两次就非常好。这使得这篇文章中的所有四点都不正确(在这里应用)。 – 2015-02-11 15:43:27

0

我只是做这个bash脚本没有问题

#!/bin/bash 
diff <(ssh [email protected]_host cat remote_file.txt) <(ssh [email protected]_host2 cat remote_file2.txt) 
exit 0 

此工作与下条件:
-remote_host和remote_host2停留在列表中~/.ssh/known_hosts
-user和user2存在,并且有权
-remote_host和remote_host2是手术,并且对
- 用户@远程主机和user2 @ remote_host2已经将ssh配置工作没有密码
SSH服务器,如果你不知道如何做这种见http://www.linuxproblem.org/art_9.html

也许你的错误留在',但它必须很好地工作,如果你不使用'remote_file.txt'只有使用remote_file.txt