简体   繁体   English

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

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

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.也许我这样做不正确,也请提供替代解决方案。

尝试这个:

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

ssh shouldn't make a difference: ssh不应该有什么不同:

$ cat do_x 
#!/bin/sh

arg1=$1
arg2=$2
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

ccarton答案中的这个变体似乎也很有效:

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

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

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