bash python子进程调用bash脚本 - 也需要打印引号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4798331/
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
python subprocess calling bash script - need to print the quotes out too
提问by Chasester
I'm having a problem with subprocess and printing quotes.
我在子流程和打印报价方面遇到问题。
My Python script takes user input, mashes it around a bit - and I need it to send it's results to a bash script in this manner.
我的 Python 脚本接受用户输入,将其混为一谈 - 我需要它以这种方式将结果发送到 bash 脚本。
myscript.sh 'var1 == a var2 == b; othervar == c' /path/to/other/files
Where I'm getting hung up on is the single quotes. Python tries to rip them out.
我被挂断的地方是单引号。Python 试图将它们撕掉。
I used this for my test.
我用这个来做我的测试。
subprocess.Popen([myscript.sh 'var=11; ignore all' /path/to/files], shell=True, executable="/bin/bash")
which returns an invalid syntax pointing at the 2nd single quote. I've also tried the above without the brackets and using single quotes outside and double quotes inside, etc.
它返回指向第二个单引号的无效语法。我也试过上面没有括号并在外面使用单引号和在里面使用双引号等。
Other - would-like.
其他- 想。
As I was saying above the 'var == a var == b; othervar == c' is derived from the python script (in string format) - and I'll need to call that in the subprocess like this.
正如我上面所说的 'var == a var == b; othervar == c' 派生自 python 脚本(以字符串格式) - 我需要在子进程中像这样调用它。
subprocess.Popen([myscript.sh myvariables /path/to/files], shell=True, executable="/bin/bash")
I just have to put the single quotes around the value of myvariables like the first example.
我只需要像第一个示例一样将单引号放在 myvariables 的值周围。
Any pointers as to where I'm going off the correct method?
关于我要去哪里使用正确方法的任何指示?
Thank you.
谢谢你。
回答by Ken Kinder
When shell=True is passed to Popen, you pass whatever you would send on the command line. That means your list should only have one element. So for example:
当 shell=True 传递给 Popen 时,你传递任何你将在命令行上发送的内容。这意味着您的列表应该只有一个元素。例如:
subprocess.Popen(['myscript.sh "var=11; ignore all" /path/to/files'], shell=True, executable="/bin/bash")
Or if /path/to/files is a variable in your Python environment:
或者,如果 /path/to/files 是 Python 环境中的变量:
subprocess.Popen(['myscript.sh "var=11; ignore all" %s' % path_to_files], shell=True, executable="/bin/bash")
Having said that I STRONGLYencourage you not to use the shell argument. The reason is fragility. You'll get a much more robust way of doing it like this:
话虽如此,我强烈建议您不要使用 shell 参数。原因是脆弱。你会得到一种更强大的方式来做这样的事情:
subprocess.Popen(["/bin/bash", "myscript.sh", "var=11; ignore all", path_to_files])
Note that "var=11; ignore all" is passed as one argument to your script. If those are separate arguments, make them separate list elements.
请注意,“var=11; ignore all”作为一个参数传递给您的脚本。如果这些是单独的参数,请将它们分开列表元素。
回答by Dave
I haven't checked why this works, but it does and without the need for shell=True.
我还没有检查为什么会这样,但它确实有效并且不需要 shell=True。
subprocess.Popen(["/bin/bash", "myscript.sh", '""' + string_to_be_quoted + '""', path_to_files])
回答by Thomas K
That's a list, those are strings in it, so they need quotes:
这是一个列表,其中包含字符串,因此它们需要引号:
["myscript.sh", "var=11; ignore all", "/path/to/files"]
That should work. If your script really somehow relies on quotes, then try this (I don't know the details of how subprocess works):
那应该有效。如果你的脚本真的以某种方式依赖于引号,那么试试这个(我不知道子进程如何工作的细节):
["myscript.sh", "'var=11; ignore all'", "/path/to/files"]

