如何在ssh命令行中转义远程命令的引号
我在远程服务器上执行以下命令时遇到了问题。命令中的 | awk '{print $1}' 似乎对输出没有任何影响。我是不是在转义引号的时候搞错了?更糟糕的是,这两个命令实际上是通过一个Python脚本提交的……所以转义就变得更加复杂了……
在本地服务器上:
ssh remote.server.com "find /root/directory -type f -exec md5sum {} + | awk '{print $1}'"
在远程服务器上:
find /root/directory -type f -exec md5sum {} + | awk '{print $1}'
2 个回答
4
你可以把 $1
转义成 \$1
,或者在本地运行 awk:
ssh remote.server.com "find /root/directory -type f -exec md5sum {} +" | awk '{print $1}'
结果应该是一样的。
8
让我们来问问 shellcheck 吧:
In yourscript line 1:
ssh remote.server.com "[...] | awk '{print $1}'"
^-- SC2029: Note that, unescaped, this
expands on the client side
这样就可以了。你可以用反斜杠来转义它。