通过stdin将带有参数的脚本作为字符串传递给bash

通过stdin将带有参数的脚本作为字符串传递给bash,bash,stdin,Bash,Stdin,目前,我在向bash传递带有参数的脚本时遇到了一个问题。限制是我可以将它们作为字符串传递(请参见$COMMAND) sshuser@server“bash-s”foo.sh&&chmod u+x foo.sh&./foo.sh bar baz”#将foo.sh的内容放在这里 `把它们放在这里 `herdoc>echo“参数1:$1” `herdoc>echo“参数2:$2” `heredoc>EOF user@server的密码: 论据1:酒吧 论据2:baz 编辑: david@localh

目前,我在向bash传递带有参数的脚本时遇到了一个问题。限制是我可以将它们作为字符串传递(请参见$COMMAND)

sshuser@server“bash-s”<“$COMMAND”
如果我的$命令包含“foo.sh bar baz”,我将得到错误
没有这样的文件或目录:foo.sh bar baz

此外,我还可以为bash设置其他选项


提前谢谢你

从错误的声音来看,
foo.sh
在服务器上不存在,所以它失败了

最简单的解决方案似乎是:

sshuser@server“foo.sh bar baz”
,前提是服务器上存在“foo.sh”

如果“foo.sh”在服务器上不存在,您是否尝试过使用?例如:

ssh user@server "cat > foo.sh && chmod u+x foo.sh && ./foo.sh bar baz" << "EOF"
`heredoc> #Put your contents of foo.sh here
`heredoc> #And put them here
`heredoc> echo "Argument 1: $1"
`heredoc> echo "Argument 2: $2"
`heredoc> EOF
user@server's password:
Argument 1: bar
Argument 2: baz
sshuser@server“cat>foo.sh&&chmod u+x foo.sh&./foo.sh bar baz”#将foo.sh的内容放在这里
`把它们放在这里
`herdoc>echo“参数1:$1”
`herdoc>echo“参数2:$2”
`heredoc>EOF
user@server的密码:
论据1:酒吧
论据2:baz
编辑:

david@localhost~%bash-s

编辑我的上一个答案,这个问题是由于引用了抛出给Bash的输入参数,因为它将整个输入视为一个文件(它正在寻找一个字面上称为“foo.sh bar baz”的文件,其中包含空格。

从错误的声音来看,
foo.sh
在服务器上不存在,所以它失败了

最简单的解决方案似乎是:

sshuser@server“foo.sh bar baz”
,前提是服务器上存在“foo.sh”

如果服务器上不存在“foo.sh”,您是否尝试过使用?例如:

ssh user@server "cat > foo.sh && chmod u+x foo.sh && ./foo.sh bar baz" << "EOF"
`heredoc> #Put your contents of foo.sh here
`heredoc> #And put them here
`heredoc> echo "Argument 1: $1"
`heredoc> echo "Argument 2: $2"
`heredoc> EOF
user@server's password:
Argument 1: bar
Argument 2: baz
sshuser@server“cat>foo.sh&&chmod u+x foo.sh&./foo.sh bar baz”#将foo.sh的内容放在这里
`把它们放在这里
`herdoc>echo“参数1:$1”
`herdoc>echo“参数2:$2”
`heredoc>EOF
user@server的密码:
论据1:酒吧
论据2:baz
编辑:

david@localhost~%bash-s

根据我之前的回答,这个问题是由于引用了抛出给Bash的输入参数,因为它将整个输入视为一个文件(它正在寻找一个字面上称为“foo.sh bar baz”的文件),其中包含空格。

根本问题是
根本问题是
嗨。感谢您的回复。ssh没有问题。您可以通过使用
foo.sh
脚本并执行
bash-s<“foo.sh bar baz”来尝试使用bash
您将看到错误。非常感谢您的帮助。是的,这会起作用,但请记住我的命令用引号“$command”包装。我想知道是否可以显式标记参数。类似于“foo.sh arg_1=bar arg_2=baz”您好。谢谢您的回复。ssh没有问题。您可以用bash试试,使用
foo.sh
script并执行
bash-s
foo.sh-bar-baz
您将得到错误。非常感谢您的帮助。是的,这会起作用,但请记住我的命令用引号“$command”包装。我想知道是否可以显式标记参数。类似于“foo.sh arg_1=bar arg_2=baz”嘿,谢谢你看这里。谢谢你的详细解释。目的是运行本地脚本并将本地参数传递给远程服务器。没关系,因为这两种方法都不可能。嘿,谢谢你看这里。谢谢你的详细解释。目的是运行本地脚本并将本地参数传递到远程服务器。没关系,因为这两种方法都不可能,我必须寻找其他方法。
david@localhost ~ % bash -s < test.sh David Finder
Hello David
Hello Finder
ssh user@server 'bash -s bar baz' <"foo.sh"
ssh user@server "bash -s $scriptargs" <"$scriptfile"