2015-02-11 262 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