为什么 ssh localhost sh -c 'echo "$0"' arg0 不输出任何内容?

Pat*_*atS 4 shell ssh

类似于为什么 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 的情况下运行按预期工作

与上面相同,但删除了 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

初始代码

\n
ssh localhost sh -c 'echo "0=$0 1=$1"' arg1 arg2 arg3\n
Run Code Online (Sandbox Code Playgroud)\n

本地 shell 扩展之后但命令执行之前

\n
ssh 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 中

\n
sh -c echo 0= 1= arg1 arg2 arg3\n#          ^^^^^ a single token\n#     ^^^^ argument for -c\n
Run Code Online (Sandbox Code Playgroud)\n

getsecho绑定到sh -c作为要执行的命令,其余部分是参数。

\n

你应该得到一个空行。

\n

就我个人而言,我了解到ssh将其参数直接传递到远程 shell,这就是它被执行的地方。因此,我倾向于编写ssh这样的命令,这提醒我该some command line\xe2\x80\xa6部分将逐字传递到远程 shell 来执行,就像我直接键入它一样

\n
ssh remoteHost 'some command line\xe2\x80\xa6'\n
Run Code Online (Sandbox Code Playgroud)\n

(将该行用单引号括起来以避免局部求值,或使用双引号来插入局部变量。)

\n
\n

然后你说实际上你正在尝试让 ssh 命令将根文件复制到另一个系统

\n
file=/etc/hosts\nssh -n host1 "scp -p '$file' host2:'/path/to/destination'"\n
Run Code Online (Sandbox Code Playgroud)\n

或者,由于文件名是安全的,并且假设您在本地客户端 和host1以及 fromhost1到之间具有根等效性host2

\n
ssh -n root@host1 scp -p /etc/hosts host2:/etc/hosts\n
Run Code Online (Sandbox Code Playgroud)\n

而现代的则scp可以进一步简化:

\n
scp -OR -p root@host1:/etc/hosts root@host2:/etc/\n
Run Code Online (Sandbox Code Playgroud)\n

  • @user253751 `ssh` _does_ 请求远程 shell。根据定义 (3认同)
  • 说实话我从来没有考虑过这一点。我了解到“ssh”将其参数传递给远程 shell,这就是它被执行的地方。因此,我倾向于编写“ssh remoteHost 'some command line...'”,这提醒我“some command line...”部分将被逐字传递到远程 shell 来执行_就像我直接键入它一样_。(将该行用单引号括起来以避免局部求值,或使用双引号来插入局部变量。)我将更新我的问题以包含此内容。 (2认同)

Tob*_*ght 5

如果您添加-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)