【发布时间】:2018-07-26 21:28:16
【问题描述】:
假设我在远程服务器example.com 上的~/echo.sh 有一个包含以下内容的脚本:
#!/usr/bin/env bash
echo "Arg 1: $1"
echo "Arg 2: $2"
echo "Arg 3: $3"
echo "Arg 4: $4"
现在,在我的本地机器上,我有另一个脚本,remote-echo.sh,其内容如下:
#!/usr/bin/env bash
ssh example.com "~/echo.sh $*"
这个想法是用户应该能够使用任何参数运行remote-echo.sh,并将这些参数转发到远程服务器上的~/echo.sh。
很遗憾,这不起作用:
$ ./remote-echo.sh "arg with space" "argwith\"quote" "arg3"
bash: -c: line 0: unexpected EOF while looking for matching `"'
bash: -c: line 1: syntax error: unexpected end of file
我该如何解决这个问题?我已经尝试使用$@ 而不是$*,但这对结果根本没有任何影响。
【问题讨论】:
-
我相信这个问题已经在知识库中,顺便说一句,具有相同的 (
printf '%q ') 答案......以及其他一些更便携的答案(因为 bash 的printf %q不能保证生成对/bin/sh有效的输出)。 -
...出于这个原因,我有时会使用 Python 的
pipes.quote()函数(在 Python 2 中,shlex.quote()在 Python 3 中)代替,当不确定哪个 shell 将被使用时在远端。 -
...链接的重复项,stackoverflow.com/questions/45308395/… 特别涵盖了这种情况。