Linux 使用 SSH 时转义引号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6621777/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 05:00:11  来源:igfitidea点击:

Escaping quotes when using SSH

linuxbashsshquoting

提问by fedeisas

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.

我正在尝试为我的 PHP 应用程序构建一个简单的部署脚本。我知道有几种工具可以完成这项工作(Capistrano、Phing 等),但对于我的简单部署例程来说,它们似乎需要做很多工作。

I use sshpassto 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来避免一遍又一遍地输入密码。但是在上传我的压缩安装程序后,我需要 ssh 进入服务器并运行一些命令。其中之一是sed。所以,引号破坏了我的脚本。它是这样的:

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.

如您所见,我使用双引号来开始我的 SSH 语句,但我也需要在 sed 上使用它们。

Any suggestions would be greatly appreciated. Thanks!

任何建议将不胜感激。谢谢!

采纳答案by Flimzy

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

"

回答by wallyk

Can a here-document be used instead?:

可以使用 here-document 代替吗?:

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