curl方式执行脚本时传参问题

2017/07/25 translate

翻译+润色

passing-parameters-to-bash-when-executing-a-script-fetched-by-curl

应用场景:使用curl执行远程Web设备上的脚本且需要携带参数时。

通常执行发布机上的脚本时习惯使用以下方式:

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 arg2

若参数中带有”-“,则可使用长选项”–“解决

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输入的BASH脚本。

echo 'i=1; for a in $@; 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

Search

    Table of Contents