我想在“Bash - How to pass arguments to a script that is read via standard input”帖子上进一步扩展。
我想创建一个采用标准输入的脚本,并在向其传递参数的同时远程运行它。
我正在构建的脚本的简化内容:
ssh server_name bash <&0
如何采用以下接受参数的方法并将其应用于我的脚本?
cat script.sh | bash /dev/stdin arguments
也许我这样做不正确,请提供替代解决方案。
答案 0 :(得分:15)
试试这个:
cat script.sh | ssh some_server bash -s - <arguments>
答案 1 :(得分:2)
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
答案 2 :(得分:0)