bash 你如何使用 python 的 os.system() “echo”引用?

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

How do you "echo" quotes using python's os.system()?

pythonbashpython-2.7

提问by insecure-IT

I'm trying to write to a monit config file using standard bash scripting inside if python's os.system(), this string is what I'd like to mimic.

我正在尝试使用标准 bash 脚本写入 monit 配置文件,如果 python 的os.system(),这个字符串是我想要模仿的。

echo -e "\t" start program = \""/etc/init.d/snortd00 start\"" >> /etc/monit.d/ips_svcs.monit

Here are my attempts using os.system(). They all produce the same results. None of which are writing the quotes around /etc/init.d/snortd00 start

这是我尝试使用os.system(). 它们都产生相同的结果。没有一个在写引号/etc/init.d/snortd00 start

   os.system('echo -e \"\t\" start program = \""/etc/init.d/snortd00 start\"" >> /etc/monit.d/ips_svcs.monit')

   os.system('echo -e \"\t\" start program = \"\"/etc/init.d/snortd00 start\"\" >> /etc/monit.d/ips_svcs.monit')

   os.system('echo -e \"\t\" start program = "/etc/init.d/snortd00 start" >> /etc/monit.d/ips_svcs.monit')

   os.system('echo -e \"\t\" start program = "\"/etc/init.d/snortd00 start\"" >> /etc/monit.d/ips_svcs.monit')

This is what is being written using all four os.system()statments. start program = /etc/init.d/snortd00 start

这就是使用所有四个os.system()语句编写的内容。start program = /etc/init.d/snortd00 start

I'm looking for this start program = "/etc/init.d/snortd00 start"

我在找这个 start program = "/etc/init.d/snortd00 start"

回答by juhist

Let's consider why your existing approaches are not working:

让我们考虑一下为什么您现有的方法不起作用:

In this case, the \is processed by Python, so the shell gets two consecutive " characters. The shell sees ""/etc..."":

在这种情况下,\由 Python 处理,因此 shell 获得两个连续的 " 字符。shell 看到""/etc...""

os.system('echo -e \"\t\" start program = \""/etc/init.d/snortd00 start\"" >> /etc/monit.d/ips_svcs.monit')

This is the same as previous: the \is processed by Python:

这和前面一样:\由 Python 处理:

os.system('echo -e \"\t\" start program = \"\"/etc/init.d/snortd00 start\"\" >> /etc/monit.d/ips_svcs.monit')

In this case, the shell sees "/etc...":

在这种情况下,shell 会看到“/etc...”:

os.system('echo -e \"\t\" start program = "/etc/init.d/snortd00 start" >> /etc/monit.d/ips_svcs.monit')

In this case also, Python processes \and the shell sees ""/etc..."":

在这种情况下,Python 处理\并且 shell 看到""/etc...""

os.system('echo -e \"\t\" start program = "\"/etc/init.d/snortd00 start\"" >> /etc/monit.d/ips_svcs.monit')

Now, what you want:

现在,你想要什么:

os.system('echo -e \"\t\" start program = \"/etc/init.d/snortd00 start\" >> /etc/monit.d/ips_svcs.monit')

Here, Python processes \\into \and the Shell sees \", which invokes the escaping mechanism of the shell so echoreally sees ".

在这里,Python 处理\\into\并且 Shell sees \",它调用了 shell 的转义机制所以echo真正看到了"

回答by Tom Fenech

Just use a raw string to avoid double-escaping (once for python, once for the shell):

只需使用原始字符串即可避免双重转义(一次用于 python,一次用于 shell):

cmd = r'echo -e "\t" start program = \""/etc/init.d/snortd00 start\"" >> /etc/monit.d/ips_svcs.monit'
os.system(cmd)

As tripleee points out in the comments, os.systemis being replaced by subprocess, so the code above would change to this:

正如 Tripeeee 在评论中指出的那样,os.system正在被subprocess替换,因此上面的代码将更改为:

subprocess.call(cmd, shell=True)

Better yet, just use python:

更好的是,只需使用python:

with open("/etc/monit.d/ips_svcs.monit", "a") as file:
    file.write('\t  start program = "/etc/init.d/snortd00 start"\n')