类似于为什么 echo 调用为 /bin/sh -c echo foo 不输出任何内容?但使用ssh
.
为什么这不输出任何内容:
ssh localhost sh -c 'echo "0=$0 1=$1"' arg1 arg2 arg3
Run Code Online (Sandbox Code Playgroud)
但如果我把它改成
ssh localhost sh -c 'true; echo "0=$0 1=$1"' arg1 arg2 arg3
# Output is
0=bash 1= arg1 arg2 arg3
Run Code Online (Sandbox Code Playgroud)
我看到的行为暗示正在运行 echo 命令,但变量替换的工作方式与预期不符。请参阅https://unix.stackexchange.com/a/253424/119816
与上面相同,但删除了 ssh 命令
sh -c 'echo "0=$0 1=$1"' arg1 arg2 arg3
# Output is
0=arg1 1=arg2
# Adding true gives same output
sh -c 'true; echo "0=$0 1=$1"' arg1 arg2 arg3
0=arg1 1=arg2
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用 ssh 命令将根文件复制到另一个系统。我正在尝试的命令是:
file=/etc/hosts
set -x
ssh host1 sudo cat $file | ssh host2 sudo sh -c 'exec cat "$0"' $file
# Output is
+ ssh host2 sudo sh -c 'exec cat > "$0"' /etc/hosts
+ ssh host1 sudo cat /etc/hosts
bash: : No such file or directory
Run Code Online (Sandbox Code Playgroud)
这对我来说看起来没问题,但我不知道如何排除故障。
我的解决方案是回到我之前使用过的
ssh host1 sudo cat $file | ssh host2 sudo tee $file > /dev/null
Run Code Online (Sandbox Code Playgroud)
以上有效。
我遇到了这个问题并提出了这个问题:
其他人在sh -c
命令方面遇到了问题/疑问:
但使用 ssh 时一定会发生一个微妙的情况,这会导致额外的 shell 评估。
roa*_*ima 11
这与您提到的问题是同一个问题。本地 shell 删除一层引号。这是发生的事情
\n初始代码
\nssh localhost sh -c 'echo "0=$0 1=$1"' arg1 arg2 arg3\n
Run Code Online (Sandbox Code Playgroud)\n本地 shell 扩展之后但命令执行之前
\nssh localhost sh -c echo "0=$0 1=$1" arg1 arg2 arg3\n# ^^^^^^^^^^^^^^^^ a single token\n# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ passed to the remote shell\n
Run Code Online (Sandbox Code Playgroud)\n在 shell 扩展之后但执行之前的远程 shell 中
\nsh -c echo 0= 1= arg1 arg2 arg3\n# ^^^^^ a single token\n# ^^^^ argument for -c\n
Run Code Online (Sandbox Code Playgroud)\ngetsecho
绑定到sh -c
作为要执行的命令,其余部分是参数。
你应该得到一个空行。
\n就我个人而言,我了解到ssh
将其参数直接传递到远程 shell,这就是它被执行的地方。因此,我倾向于编写ssh
这样的命令,这提醒我该some command line\xe2\x80\xa6
部分将逐字传递到远程 shell 来执行,就像我直接键入它一样:
ssh remoteHost 'some command line\xe2\x80\xa6'\n
Run Code Online (Sandbox Code Playgroud)\n(将该行用单引号括起来以避免局部求值,或使用双引号来插入局部变量。)
\n然后你说实际上你正在尝试让 ssh 命令将根文件复制到另一个系统
\nfile=/etc/hosts\nssh -n host1 "scp -p '$file' host2:'/path/to/destination'"\n
Run Code Online (Sandbox Code Playgroud)\n或者,由于文件名是安全的,并且假设您在本地客户端 和host1
以及 fromhost1
到之间具有根等效性host2
:
ssh -n root@host1 scp -p /etc/hosts host2:/etc/hosts\n
Run Code Online (Sandbox Code Playgroud)\n而现代的则scp
可以进一步简化:
scp -OR -p root@host1:/etc/hosts root@host2:/etc/\n
Run Code Online (Sandbox Code Playgroud)\n
如果您添加-v
到命令中ssh
,它将显示它正在做什么:
debug1: Sending command: sh -c echo "0=$0 1=$1" arg1 arg2 arg3
Run Code Online (Sandbox Code Playgroud)
果然,如果我们sh
在本地运行该命令,我们只会得到一个空行输出,因为sh
正在运行echo
并"0=$0 1=$1"
成为第零个参数。
当我们在前面添加 时true;
,会发生这种情况:
debug1: Sending command: sh -c true; echo "0=$0 1=$1" arg1 arg2 arg3
Run Code Online (Sandbox Code Playgroud)
现在sh
只是运行true
。然后echo
输出它的参数:
0=bash 1= arg1 arg2 arg3
Run Code Online (Sandbox Code Playgroud)
你可能想要的是跑到sh -c 'echo "0=$0 1=$1"' arg1 arg2 arg3
另一边。您需要引用整个命令,以便远程 shell 完整接收它:
ssh localhost "sh -c 'echo \"0=\$0 1=\$1\"' arg1 arg2 arg3"
Run Code Online (Sandbox Code Playgroud)
输出:
0=arg1 1=arg2
Run Code Online (Sandbox Code Playgroud)
如果您有 GNU printf
(在本地端),您可以通过使用它来格式化适合远端的命令字符串,从而使其更具可读性:
command='echo "0=$0 1=$1"'
ssh localhost "sh -c $(printf %q "$command") arg1 arg2 arg3"
Run Code Online (Sandbox Code Playgroud)