wget bash 带参数的脚本

wget bash script with parameters

我是 运行 一个使用

的 bash 脚本
wget -O - https://myserver/install/Setup.sh | bash

如何将参数传递给上述脚本使其运行?像

wget -O - https://myserver/install/Setup.sh parameter1 | bash

bash(或sh 或类似)命令的标准格式是bash scriptfilename arg1 arg2 ...。如果您省略所有第一个参数(运行 的脚本名称或路径),它会从标准输入读取脚本。不幸的是,没有办法放弃第一个参数而忽略其他参数。幸运的是,您可以将 /dev/stdin 作为第一个参数传递并获得相同的效果(至少在大多数 unix 系统上):

wget -O - https://myserver/install/Setup.sh | bash /dev/stdin parameter1

如果您使用的系统没有 /dev/stdin,您可能需要寻找其他方法来显式指定标准输入(/dev/fd/0 或类似的东西)。

编辑:Léa Gris bash -s arg1 arg2 ... 的建议可能是更好的方法。

您还可以 运行 您的脚本:

wget -qO - 'https://myserver/install/Setup.sh' | bash -s parameter1

参见:man bash OPTIONS -s

   -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 or
             when reading input through a pipe.

或者使用 -c 选项。

bash -c "$(wget -qO - 'https://myserver/install/Setup.sh')" '' parameter1

'' 定义参数 [=18=] 为空字符串。在普通的基于文件的脚本调用中,参数 [=18=] 包含调用者脚本名称。

参见:man bash OPTIONS -c

   -c        If the -c option is present, then commands are read from
             the first non-option argument command_string.  If there are
             arguments after the command_string, the first argument is
             assigned to [=13=] and any remaining arguments are assigned to
             the positional parameters.  The assignment to [=13=] sets the
             name of the shell, which is used in warning and error
             messages.