Escaping quotes when using SSH

大兔子大兔子 提交于 2019-12-29 07:35:26

问题


I'm trying to build a simple deployment script for my PHP apps. I know there are several tools for this job (Capistrano, Phing, etc.) but they seem like a lot of work for my simple deployment routine.

I use sshpass to avoid typing my password over and over again. But after uploading my compressed installer, I need to ssh into the server and run some commands. One of which is sed. So, quotes are breaking my script. It's something like this:

sshpass -p foo ssh user@host "
   cd /www/htdocs/foo/bar 
   echo 'Untar and remove installer'
   tar -zxf install.tar.gz

   sed "s/define('ENVIRONMENT', 'development');/define('ENVIRONMENT', 'production');" index.php > tmp && mv tmp index.php
   sed "s/define('ENVIRONMENT', 'development');/define('ENVIRONMENT', 'production');/" admin/index.php > tmp && mv tmp admin/index.php

"

As you can see, I use double-quotes to start my SSH statements, but I also need to use them on sed.

Any suggestions would be greatly appreciated. Thanks!


回答1:


Escaping the internal quote marks is the normal way. Does this not work?

sshpass -p foo ssh user@host "
cd /www/htdocs/foo/bar
echo 'Untar and remove installer'
tar -zxf install.tar.gz

sed \"s/define('ENVIRONMENT', 'development');/define('ENVIRONMENT', 'production');\" index.php > tmp && mv tmp index.php
sed \"s/define('ENVIRONMENT', 'development');/define('ENVIRONMENT', 'production');/\" admin/index.php > tmp && mv tmp admin/index.php

"



回答2:


Can a here-document be used instead?:

sshpass -p foo ssh user@host <<DATA
   cd /www/htdocs/foo/bar 
   echo 'Untar and remove installer'
   tar -zxf install.tar.gz

   sed "s/define('ENVIRONMENT', 'development');/define('ENVIRONMENT', 'production');" index.php > tmp && mv tmp index.php
   sed "s/define('ENVIRONMENT', 'development');/define('ENVIRONMENT', 'production');/" admin/index.php > tmp && mv tmp admin/index.php
DATA


来源:https://stackoverflow.com/questions/6621777/escaping-quotes-when-using-ssh

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