我知道如何像这样执行远程 Bash 脚本:
curl http://example.com/script.sh | bash
或
bash < <( curl http://example.com/script.sh )
结果相同。
但是,如果我需要将参数 传递给 bash 脚本怎么办?当脚本保存在本地时是可能的:
./script.sh argument1 argument2
我尝试了几种类似的可能性,但没有成功:
bash < <( curl http://example.com/script.sh ) argument1 argument2
最佳答案
改进 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
这当然适用于通过 stdin 进行的任何类型的输入,而不仅仅是 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
关于bash - 在执行 curl 获取的脚本时将参数传递给 bash,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4642915/