python/shell字符串中非常复杂的引号

2024-05-23 22:05:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个很长的字符串ssh_cmd,我从

cmd = """kill -9 `ps -ef|grep "udp_receiver"|grep -v "grep"|awk '{print $2}'`"""
HostName="133.33.22.1"
ssh_cmd = """ssh -t inria_spoofing@{0} 'sudo nohup bash -c "{1} > /nohup.out 2>&1 &"'""".format(HostName, cmd)

结果ssh_cmd是:

^{pr2}$

但是,我害怕当我跑的时候

child = pexpect.spawn(ssh_cmd)

有问题 那么如何组织绳子呢? 谢谢!在


Tags: 字符串cmdgrepsshhostnamepsudpkill
1条回答
网友
1楼 · 发布于 2024-05-23 22:05:22

要回答这个问题,下面是正确的ssh_cmdssh -t kitty@133.33.22.1 "sudo nohup bash -c \"kill -9 \\\`ps -ef | grep 'udp_receiver' | grep -v 'grep' | awk '{print \\\$2}'\\\` > /nohup.out 2>&1 &\""

基本上,每次将命令嵌入另一个命令时,都需要对命令中的双引号、反撇号和反斜杠进行转义。我没有使用单引号,除非在较低级别,因为您不能在单引号内使用转义单引号。在

如果$只是用双引号引起来的字符串中的一个字符,即使该字符串也包含单引号,也需要对$进行转义。在

相关问题 更多 >