bash 转义单引号 ssh 远程命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15567847/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Escape single quotes ssh remote command
提问by abkrim
I read any solutions for escape single quotes on remote command over ssh. But any work fien.
我阅读了通过 ssh 远程命令转义单引号的任何解决方案。但任何工作狂。
I'm trying
我想
ssh root@server "ps uax|grep bac | grep -v grep | awk '{ print }' > /tmp/back.tmp"
Don't work awk
不要工作 awk
ssh root@server "ps uax|grep bac | grep -v grep | awk \'{ print }\' > /tmp/back.tmp"
....
awk: '{
awk: ^ caracter ''' inválido en la expresión
And try put single quotas on command but also don't work.
并尝试将单个配额置于命令中,但也不起作用。
Aprecite help
感谢帮助
回答by Ben
The sshcommand treats all text typed after the hostname as the remote command to executed. Critically what this means to your question is that you do not need to quote the entire command as you have done. Rather, you can just send through the command as you would type it as if you were on the remote system itself.
该ssh命令将在主机名之后键入的所有文本视为要执行的远程命令。至关重要的是,这对您的问题意味着您不需要像您所做的那样引用整个命令。相反,您可以直接发送命令,就像您在远程系统本身上键入它一样。
This simplifies dealing with quoting issues, since it reduces the number of quotes that you need to use. Since you won't be using quotes, all special bash characters need to be escaped with backslashes.
这简化了引用问题的处理,因为它减少了您需要使用的引用数量。由于您不会使用引号,因此所有特殊的 bash 字符都需要使用反斜杠进行转义。
In your situation, you need to type,
在你的情况下,你需要输入,
ssh root@server ps uax \| grep ba[c] \| \'{ print $2 }\' \> /tmp/back.tmp
or you could double quote the single quotes instead of escaping them (in both cases, you need to escape the dollar sign)
或者你可以双引号单引号而不是转义它们(在这两种情况下,你都需要转义美元符号)
ssh root@server ps uax \| grep ba[c] \| "'{ print $2 }'" \> /tmp/back.tmp
Honestly this feels a little more complicated, but I have found this knowledge pretty valuable when dealing with sending commands to remote systems that involve more complex use of quotes.
老实说,这感觉有点复杂,但我发现在处理将命令发送到涉及更复杂的引号使用的远程系统时,这些知识非常有价值。
回答by Adrian Pronk
In your first try you use double-quotes "so you need to escape the $character:
在您第一次尝试时,您使用双引号,"因此您需要对$字符进行转义:
ssh root@server "ps uax|grep bac | grep -v grep | awk '{ print $2 }' > /tmp/back.tmp"
▲
Also, you can use:
此外,您可以使用:
ps uax | grep 'ba[c]' | ...
so then you don't need the grep -v grepstep.
所以你不需要这个grep -v grep步骤。

