如何在ssh/remote bash命令中转义单引号字符? [英] How to escape the single quote character in an ssh / remote bash command?

查看:248
本文介绍了如何在ssh/remote bash命令中转义单引号字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一小组脚本,用于远程启动,停止和检查进程的状态.这些脚本的stop应该寻找一个进程并杀死它.因此,我这样做:

I'm building a small set of scripts for remotely starting, stopping and checking the status of a process. The stop of these scripts should look for a process and kill it. Therefore I do:

ssh deploy@hera 'kill -9 `ps -ef | grep MapReduceNode | grep -v "grep" | awk -F " " '{print $2}' | head -n 1`'

这里的问题是awk令牌化步骤需要单引号,并且这些引号与用于通过ssh执行远程命令的单引号冲突.这些单引号如何转义?

The problem here is that the awk tokenization step needs single quotes and these clash with the single quote utilized for executing the remote command via ssh. How can these single quotes be escaped?

推荐答案

使用

ssh deploy@hera 'kill -9 `ps -ef | grep MapReduceNode | grep -v "grep" | awk -F " " '"'"'{print $2}'"'"' | head -n 1`'

说明:

ssh deploy@hera 'kill -9 `ps -ef | grep MapReduceNode | grep -v "grep" | awk -F " " '"'"'{print $2}'"'"' | head -n 1`'
                >                               1                                   <>2<>    3     <>4<>      5      <

1)第一个以命令开头的字符串:'kill -9 `ps -ef | grep MapReduceNode | grep -v "grep" | awk -F " " '

1) First string with beginning of command: 'kill -9 `ps -ef | grep MapReduceNode | grep -v "grep" | awk -F " " '

2)第二个字符串,只有一个'char:"'"

2) Second string with only a single ' char: "'"

3)使用打印命令的第三个字符串:'{print $2}'

3) Third string with the print command: '{print $2}'

4)第四个字符串加上另一个单引号:"'"

4) Fourth string with another single quote: "'"

5)带有其余命令的第五个字符串:' | head -n 1`'

5) Fifth string with rest of command: ' | head -n 1`'

这篇关于如何在ssh/remote bash命令中转义单引号字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆