curl方式執行腳本時傳參問題

參考:

https://stackoverflow.com/questions/4642915/passing-parameters-to-bash-when-executing-a-script-fetched-by-curl/4642975


通常執行發佈機上的腳本時習慣使用以下方式:

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


若涉及到傳入參數時,則可使用

1. curl http://example.com/script.sh | bash -s arg1 arg2
2. curl http://example.com/script.sh | bash /dev/stdin arg1 arg2
3. bash <( curl http://example.com/script.sh ) arg1


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

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

若參數爲"-p blah -d blah",則可使用以下命令執行

curl http://example.com/script.sh | bash -s -- -p blah -d blah


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

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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章