我有这个示例 shell 脚本:
echo /$1/
所以我可以打电话
$ . ./script 5
# output: /5/
我想将脚本通过管道传输到 sh(ell),但我也可以传递 arg 吗?
cat script | sh
# output: //
我有这个示例 shell 脚本:
echo /$1/
所以我可以打电话
$ . ./script 5
# output: /5/
我想将脚本通过管道传输到 sh(ell),但我也可以传递 arg 吗?
cat script | sh
# output: //
-s您可以使用以下选项将参数传递给 shell :
cat script | bash -s 5
采用bash -s -- <args>
例如,安装 google cloud sdk
~ curl https://sdk.cloud.google.com | bash -s -- --disable-prompts
cat script | sh -s -- 5
该-s参数告诉sh从标准输入中获取命令,并且不需要文件名作为位置参数。(否则,如果没有-s,下一个非标志参数将被视为文件名。)
告诉 sh 停止处理进一步的--参数,以便它们只被脚本拾取(而不是应用于sh自身)。这在您需要将标志传递给脚本的情况下很有用,该标志以-or开头--(例如:)--dry-run必须被忽略sh。另请注意, single-等效于--.
cat script | sh -s - --dry-run