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)
bash(或sh类似)命令的标准格式是bash scriptfilename arg1 arg2 .... 如果省略所有第一个参数(要运行的脚本的名称或路径),它将从 stdin 读取脚本。不幸的是,我们无法忽略第一个论点而忽略其他论点。幸运的是,您可以/dev/stdin作为第一个参数传递并获得相同的效果(至少在大多数 UNIX 系统上):
wget -O - https://myserver/install/Setup.sh | bash /dev/stdin parameter1\nRun Code Online (Sandbox Code Playgroud)\n\n如果您所在的系统没有 /dev/stdin,您可能需要寻找另一种方法来显式指定 stdin(/dev/fd/0 或类似的东西)。
\n\n编辑: L\xc3\xa9a Gris 的建议bash -s arg1 arg2 ...可能是更好的方法。
您还可以使用以下命令运行脚本:
wget -qO - 'https://myserver/install/Setup.sh' | bash -s parameter1
Run Code Online (Sandbox Code Playgroud)
请参阅:man bash选项-s
Run Code Online (Sandbox Code Playgroud)-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
Run Code Online (Sandbox Code Playgroud)
在''定义了参数$0为空字符串。在基于普通文件的脚本调用中,该参数$0包含调用者脚本名称。
请参阅:man bash选项-c
Run Code Online (Sandbox Code Playgroud)-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.
| 归档时间: |
|
| 查看次数: |
1703 次 |
| 最近记录: |