wget bash script with parameters

ASh*_*hah 6 bash wget

I'm running a bash script using

wget -O - https://myserver/install/Setup.sh | bash
Run Code Online (Sandbox Code Playgroud)

How can I pass a parameter to the above script so it runs? something like

wget -O - https://myserver/install/Setup.sh parameter1 | bash
Run Code Online (Sandbox Code Playgroud)

Gor*_*son 5

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

\n\n
wget -O - https://myserver/install/Setup.sh | bash /dev/stdin parameter1\n
Run Code Online (Sandbox Code Playgroud)\n\n

如果您所在的系统没有 /dev/stdin,您可能需要寻找另一种方法来显式指定 stdin(/dev/fd/0 或类似的东西)。

\n\n

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

\n


Léa*_*ris 5

您还可以使用以下命令运行脚本:

wget -qO - 'https://myserver/install/Setup.sh' | bash -s parameter1
Run Code Online (Sandbox Code Playgroud)

请参阅:man bash选项-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.
Run Code Online (Sandbox Code Playgroud)

或者使用该-c选项。

bash -c "$(wget -qO - 'https://myserver/install/Setup.sh')" '' parameter1
Run Code Online (Sandbox Code Playgroud)

''定义了参数$0为空字符串。在基于普通文件的脚本调用中,该参数$0包含调用者脚本名称。

请参阅:man bash选项-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 $0 and any remaining arguments are assigned to
             the positional parameters.  The assignment to $0 sets the
             name of the shell, which is used in warning and error
             messages.
Run Code Online (Sandbox Code Playgroud)