繁体   English   中英

带参数的 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

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

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

请参阅: 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.

或者使用-c选项。

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

''将参数$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.

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM