如何将参数传递给sh read从stdin执行的脚本?

时间:2016-12-13 11:55:11

标签: linux bash shell

我使用wget example.com 下载一些shell脚本,然后通过管道将wget的stdout流式传输到sh的stdin来立即执行它命令。

wget -O - http://example.com/myscript.sh | sh -

如何将参数传递给脚本

2 个答案:

答案 0 :(得分:6)

您需要在调用-s时使用bash选项将参数传递给正在下载的shell脚本:

wget -O - http://example.com/myscript.sh | bash -s 'arg1' 'arg2'

根据man bash

-s   If the -s option is present, or if no arguments remain after option processing,
     then commands are  read  from the  standard  input. This option allows
     the positional parameters to be set when invoking an interactive shell.

答案 1 :(得分:1)

虽然接受的答案是正确的,但它仅在bash上有效,而在最初的发帖人要求下,不适用于sh

要在sh中执行此操作,您必须添加--

curl https://example.com/script.sh | sh -s -- --my-arg
相关问题