天天看點

curl方式執行shell腳本時傳參數

有時候shell腳本可以放在http頁面上,不用download,可以直接執行。

通常我們可以用curl的方式執行http頁面上的shell腳本。 一般方式是:

這樣腳本就可以再本地機器上執行了。

但是需要傳入參數的腳本。我們可以用下面的方式傳入shell參數

-s方式
curl -s http://sukbeta.github.io/web/shell.sh | bash -s arg1 arg2
           
< 方式
bash <(curl -s http://sukbeta.github.io/web/shell.sh) arg1 arg2
           

注意 <( 之間不要有空格!!!

若參數中帶有”-“,則可使用長選項”–”解決

curl -s http://sukbeta.github.io/web/shell.sh | bash -s -- arg1 arg2
           

若參數為”-p arg -d arg”,則可使用以下指令執行

curl -s http://sukbeta.github.io/web/shell.sh | bash -s -- -p arg1 -d arg2
           

不止是curl的輸入,其他方式的輸入也滿足。可以通過以下例子深入了解下

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