Bash - How to pass arguments to a script that is r

2019-01-23 05:49发布

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

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.

标签: bash ssh
3条回答
贼婆χ
2楼-- · 2019-01-23 05:51

This variant on ccarton's answer also seems to work well:

ssh some_server bash -s - < script.sh <arguments>
查看更多
戒情不戒烟
3楼-- · 2019-01-23 05:54

ssh shouldn't make a difference:

$ 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
查看更多
手持菜刀,她持情操
4楼-- · 2019-01-23 06:11

Try this:

cat script.sh | ssh some_server bash -s - <arguments>
查看更多
登录 后发表回答