Problems running a complicated sed command over SSH

谁都会走 提交于 2019-12-13 01:57:55

问题


I've read multiple other answers on the topic but none seem to work for my problem.

I just got an answer to my other question on how to add prefixes to 2 strings of text in a long, single-line .json file.

The command is

sed -i 's~"libraries"~"pack/\0"~; s~"objects"~"pack/\0"~' /var/www/html/pack/file.json

and it contains single and double quotes, as well as forward and double slashes, making other answers fail.

I've tried

ssh ip<<'EOF'
sed -i 's~"libraries"~"pack/\0"~; s~"objects"~"pack/\0"~' /var/www/html/pack/file.json
EOF

ssh ip 'sed -i '"'"'s~"libraries"~"pack/\0"~; s~"objects"~"pack/\0"~'"'"' /var/www/html/pack/file.json'

and everything on this page, and possibly a couple other things from answers here, with no luck.

The first command, with EOF, opens the same screen I would get when simply ssh ip-ing; a remote command prompt. The same happens with some other commands from the Zaiste.net page.

Please note that the sed command is being run from a script executed in terminal. Thus, multiple line answers are OK.

Also,

ssh ip 'sed -i '"'"'s~"libraries"~"pack/\0"~; s~"objects"~"pack/\0"~'"'"' /var/www/html/pack/file.json'

returns EXTRA quotes around the "objects" and "libraries" strings I am trying to prefix, for example

"minimumVersion":2,"librariesLocation":"pack/"libraries"","objectsLocation":"pack/"objects"",

If the extra quotes issue from this command can be resolved, that's the solution to my problem.


回答1:


You can use:

ssh -t -t ip<<'EOF'
sed -i 's~"libraries"~"pack/&"~; s~"objects"~"pack/&"~' /var/www/html/pack/file.json
exit
EOF


来源:https://stackoverflow.com/questions/32619016/problems-running-a-complicated-sed-command-over-ssh

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!