2011-01-10 68 views
28

我知道如何執行遠程bash腳本提取腳本時打壞,經由這些語法:傳遞參數執行由捲曲

curl http://foo.com/script.sh | bash 

bash < <(curl http://foo.com/script.sh) 

其中給出相同的結果。

但是如果我需要將參數傳遞給bash腳本呢?這是可能的,當腳本保存在本地:

./script.sh argument1 argument2 

我嘗試了幾種可能性,像這樣的,沒有成功:

bash < <(curl http://foo.com/script.sh) argument1 argument2 

回答

54

嘗試

curl http://foo.com/script.sh | bash -s arg1 arg2 

bash的手冊說:

如果存在-s選項,或者沒有參數在選項處理之後,然後從標準輸入中讀取命令。該選項允許在調用交互式shell時設置位置參數。

+3

謝謝!非常有用的要點:) – 2011-01-10 01:37:05

+1

如果arg1是一個簡短的arg,則不要工作:curl http://foo.com/script.sh | bash -s -y – Xorax 2012-10-26 17:40:22

+1

那些使用像`-p blah -d blah`這樣的鍵的參數呢? – CMCDragonkai 2014-05-15 06:21:46

13

其他替代方案:

curl http://foo.com/script.sh | bash /dev/stdin arguments 
bash <(curl http://foo.com/script.sh) arguments 
37

要在jinowolski's answer提高一點,你應該使用:

curl http://example.com/script.sh | bash -s -- arg1 arg2 

注意兩個破折號( - ),它告訴bash的不處理任何事情作爲bash的參數。

這樣它會與任何類型的參數工作,如:

curl -L http://bootstrap.saltstack.org | bash -s -- -M -N stable 

當然工作這將通過標準輸入任何類型的輸入,而不僅僅是捲曲,這樣你就可以確認它與簡單通過回聲bash腳本輸入:

echo 'i=1; for a in [email protected]; do echo "$i = $a"; i=$((i+1)); done' | \ 
bash -s -- -a1 -a2 -a3 --long some_text 

會給你輸出

1 = -a1 
2 = -a2 
3 = -a3 
4 = --long 
5 = some_text