简体   繁体   English

如何使带有大量引号的命令远程运行?

[英]How to have a command with lots of quotes running remotely?

I am trying to run a command-line remotely that contains some single, double quotes. 我试图远程运行包含一些单引号,双引号的命令行。

1) This is the command I want to have it running on remote host. 1)这是我想让它在远程主机上运行的命令。

  echo '{"id":12345,"name":"activate_cluster"}' 

which should be of the exactly same format. 格式应完全相同。 Not any missing characters. 没有任何缺少的字符。

2) This is the full command I have used to trigger this command from my local host: 2)这是我用来从本地主机触发此命令的完整命令:

expect bashscript $hostname $user $pwd 'echo \'\{\"id\":12345\,\"name\":\"activate_cluster\"\}\'

3) But when it reaches the remote host, this command becomes, 3)但是,当它到达远程主机时,该命令变为:

echo {"id":12345,"name":"activate_cluster"}

The pair of single quotes is gone! 双引号不见了! Is there a way I can fix this? 有办法解决这个问题吗?

You cannot embed a single quote within single quotes in bash: https://www.gnu.org/software/bash/manual/bashref.html#Single-Quotes 您不能在bash中的单引号中嵌入单引号: https : //www.gnu.org/software/bash/manual/bashref.html#Single-Quotes

You'll have to do something like this: 您将必须执行以下操作:

expect bashscript $hostname $user $pwd 'echo '\''{"id":12345,"name":"activate_cluster"}'\'
# ...........................................^^^^......................................^^^

'\\'' -- the first quote ends the opening quote from 'echo , the escaped quote appends a literal quote, and the third one opens a new quoted string. '\\'' -第一个引号以'echo开头的引号结束,转义的引号后面附加一个文字引号,第三个引号打开一个新的引号字符串。 Within single quotes, you don't need to go nuts with backslashes -- they're all literal characters in there. 用单引号括起来,您无需大意使用反斜杠,因为它们全都是文字字符。

Another approach would be to store the command in a separate variable: 另一种方法是将命令存储在单独的变量中:

cmd=$(cat <<'END'
echo '{"id":12345,"name":"activate_cluster"}' 
END
)
expect bashscript $hostname $user $pwd "$cmd"

A little wordier but much tidier, no? 有点呆板但比较整洁,不是吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM