带参数的wget bash脚本

时间:2019-07-07 00:39:32

标签: bash wget

我正在使用

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

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

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

2 个答案:

答案 0 :(得分:1)

bash(或sh或类似的命令)的标准格式为bash scriptfilename arg1 arg2 ...。如果不使用所有第一个参数(要运行的脚本的名称或路径),它将从stdin中读取脚本。不幸的是,没有任何办法可以摆脱firs的争论,而要超越其他人。幸运的是,您可以将/dev/stdin作为第一个参数,并获得相同的效果(至少在大多数Unix系统上):

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

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

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

答案 1 :(得分:1)

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

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

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

请参阅: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 $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.
相关问题