2015-06-25 40 views
0

我有一個運行帶參數的shell腳本的問題。 這個命令直接在Linux上運行的工作原理:外殼通信參數問題

comm -13 <(sort /tmp/f1.txt) <(sort /tmp/f2.txt) > /tmp/f3.txt 

如果我嘗試使用以下命令運行這個shell腳本發送參數和我得到以下錯誤:

test.sh: line 6: syntax error near unexpected token `(' 
'est.sh: line 6: `comm -13 <(sort $1) <(sort $2) > $3 

這裏是我的殼代碼:

#!/bin/bash 
comm -13 <(sort $1) <(sort $2) > $3 

我用下面的命令來運行它:

sh test.sh /tmp/f1.txt /tmp/f2.txt /tmp/f3.txt 

我已經跑出了想法可能是錯誤的。 請協助。

謝謝 -Andrey

+1

'sh'不是''bash' <(...)'是一個bash的功能。不要使用'sh'來運行腳本。使用'bash'。 –

+1

原因可能是,無論你的系統上有什麼sh,都意味着它不支持[進程替換](https://www.gnu.org/software/bash/manual/html_node/Process-Substitution.html#Process-代換)。你爲什麼要在你的腳本中放入'#!/ bin/bash'並使用'sh'來執行它?看到這個:https://www.gnu.org/software/bash/manual/html_node/Major-Differences-From-The-Bourne-Shell.html –

+0

這工作。非常感謝你們。 – Andrey

回答

1

解決方案:

  1. 既然你已經指定在bash腳本的shebang,你爲什麼用sh打電話嗎?只需運行./test.sh /tmp/f1.txt /tmp/f2.txt /tmp/f3.txt

  2. 使用bash明確:bash test.sh /tmp/f1.txt /tmp/f2.txt /tmp/f3.txt

+0

這工作。非常感謝! – Andrey