Bash - 如何将参数传递给通过重定向标准输入读取的脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8528716/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 01:10:52  来源:igfitidea点击:

Bash - How to pass arguments to a script that is read via redirected standard input

bashssh

提问by dabest1

I would like to expand a little more on "Bash - How to pass arguments to a script that is read via standard input" post.

我想对“ Bash - 如何将参数传递给通过标准输入读取的脚本”一文进行更多的扩展。

I would like to create a script that takes standard input and runs it remotely while passing arguments to it.

我想创建一个脚本,它接受标准输入并在向它传递参数的同时远程运行它。

Simplified contents of the script that I'm building:

我正在构建的脚本的简化内容:

ssh server_name bash <&0

How do I take the following method of accepting arguments and apply it to my script?

如何采用以下接受参数的方法并将其应用于我的脚本?

cat script.sh | bash /dev/stdin arguments

Maybe I am doing this incorrectly, please provide alternate solutions as well.

也许我这样做不正确,也请提供替代解决方案。

回答by ccarton

Try this:

尝试这个:

cat script.sh | ssh some_server bash -s - <arguments>

回答by Brian Cain

sshshouldn't make a difference:

ssh应该没有区别:

$ cat do_x 
#!/bin/sh

arg1=
arg2=
all_cmdline=$*
read arg2_from_stdin

echo "arg1: ${arg1}"
echo "arg2: ${arg2}"
echo "all_cmdline: ${all_cmdline}"
echo "arg2_from_stdin: ${arg2_from_stdin}"

$ echo 'a b c' > some_file
$ ./do_x 1 2 3 4 5 < some_file 
arg1: 1
arg2: 2
all_cmdline: 1 2 3 4 5
arg2_from_stdin: a b c
$ ssh some-server do_x 1 2 3 4 5 < some_file
arg1: 1
arg2: 2
all_cmdline: 1 2 3 4 5
arg2_from_stdin: a b c

回答by sampablokuper

This variant on ccarton's answeralso seems to work well:

在这种变体ccarton答案也似乎运作良好:

ssh some_server bash -s - < script.sh <arguments>