2017-09-15 67 views
0

全部, 我写入文件到远程节点时,我还没有能够逃脱单引号。远程ssh执行:双引号内单引号

ssh -i demo.pem -t [email protected] 'echo '\''{"watches": [{"type": "key","key": "test","handler": "neon -e 'sudo /opt/watch_handler.sh'"}]}'\''| sudo tee /etc/key.json' 

我得到的输出如下。

{"watches": [{"type": "key","key": "test","handler": "neon -e sudo /opt/watch_handler.sh"}]} 

我想输出有大约 '须藤/opt/watch_handler.sh'

{"watches": [{"type": "key","key": "test","handler": "neon -e 'sudo /opt/watch_handler.sh'"}]} 

单引号 '\' 是行不通的。

请你帮忙。

谢谢,

回答

0

获取多级引用正确是麻烦和容易出错的。考虑其他解决方案,如:

cat <<EOF | ssh -i demo.pem -t [email protected] sudo tee /etc/key.json 
{"watches": [{"type": "key","key": "test","handler": "neon -e 'sudo /opt/watch_handler.sh'"}]} 
EOF 

我喜欢用cat,因为它不需要任何逃避工作。但是,只要您在JSON表达式中转义双引号,您也可以使用echo而不是cat在本地生成字符串:

echo "{\"watches\": [{\"type\": \"key\",\"key\": \"test\",\"handler\": \"neon -e 'sudo /opt/watch_handler.sh'\"}]}" | sudo tee /etc/key.json