Python 为什么我的变量不包括在subprocess.Popen中?
我只是想把一个变量传递给我的shell脚本,但它并没有被传递。我已经 下面是python文档中的示例,但它不起作用。我错过了什么Python 为什么我的变量不包括在subprocess.Popen中?,python,subprocess,Python,Subprocess,我只是想把一个变量传递给我的shell脚本,但它并没有被传递。我已经 下面是python文档中的示例,但它不起作用。我错过了什么 subprocess.Popen(['./script.sh' + variable] , shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) 在./script.sh之后添加空格: 在./script.sh之后添加空格: 在./script.sh之后添加空格: 在./script.sh之后添加
subprocess.Popen(['./script.sh' + variable] , shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
在./script.sh之后添加空格:
在./script.sh之后添加空格:
在./script.sh之后添加空格:
在./script.sh之后添加空格:
只需在脚本名称后添加一个空格:
subprocess.Popen(['./script.sh ' + variable], shell=True,
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
只需在脚本名称后添加一个空格:
subprocess.Popen(['./script.sh ' + variable], shell=True,
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
只需在脚本名称后添加一个空格:
subprocess.Popen(['./script.sh ' + variable], shell=True,
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
只需在脚本名称后添加一个空格:
subprocess.Popen(['./script.sh ' + variable], shell=True,
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
您不应该在这里使用
shell=True
,除非您希望执行变量中的任何实际shell语法(如>file.log
)
subprocess.Popen(['./script.sh', variable],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
如果您真的想要
shell=True
,您有几个安全的选项。第一种方法是使用pipes.quote()
(或者在Python 3中使用shlex.quote()
)来防止shell转义:
subprocess.Popen('./script.sh ' + pipes.quote(variable), shell=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
第二个是将名称作为后续参数传递(注意空字符串,它在生成的shell中变成$0
):
记住,不仅仅是SQL,还有他的妹妹
Susan$(rm-rf/)
也在那里。您根本不应该在这里使用shell=True
,除非您希望在变量中执行任何实际的shell语法(如>file.log
)
subprocess.Popen(['./script.sh', variable],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
如果您真的想要
shell=True
,您有几个安全的选项。第一种方法是使用pipes.quote()
(或者在Python 3中使用shlex.quote()
)来防止shell转义:
subprocess.Popen('./script.sh ' + pipes.quote(variable), shell=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
第二个是将名称作为后续参数传递(注意空字符串,它在生成的shell中变成$0
):
记住,不仅仅是SQL,还有他的妹妹
Susan$(rm-rf/)
也在那里。您根本不应该在这里使用shell=True
,除非您希望在变量中执行任何实际的shell语法(如>file.log
)
subprocess.Popen(['./script.sh', variable],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
如果您真的想要
shell=True
,您有几个安全的选项。第一种方法是使用pipes.quote()
(或者在Python 3中使用shlex.quote()
)来防止shell转义:
subprocess.Popen('./script.sh ' + pipes.quote(variable), shell=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
第二个是将名称作为后续参数传递(注意空字符串,它在生成的shell中变成$0
):
记住,不仅仅是SQL,还有他的妹妹
Susan$(rm-rf/)
也在那里。您根本不应该在这里使用shell=True
,除非您希望在变量中执行任何实际的shell语法(如>file.log
)
subprocess.Popen(['./script.sh', variable],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
如果您真的想要
shell=True
,您有几个安全的选项。第一种方法是使用pipes.quote()
(或者在Python 3中使用shlex.quote()
)来防止shell转义:
subprocess.Popen('./script.sh ' + pipes.quote(variable), shell=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
第二个是将名称作为后续参数传递(注意空字符串,它在生成的shell中变成$0
):
记住,不仅仅是SQL,还有他的妹妹
Susan$(rm-rf/)
也在那里。你正在结合两种不同的做事方式。最重要的是,你做错了,但仅仅纠正“做错了”并不是答案
您可以将两个参数放在一个列表中,然后在不使用shell的情况下启动它,如['./script.sh',variable]
。这通常更好。使用shell意味着您必须处理引用、意外或恶意注入,可能会干扰您的输入和输出,并增加性能成本。所以,如果你不需要它,就不要使用它
或者,您可以将两个参数放在一个字符串中,然后使用shell启动它,如'./script.sh'+variable
但不能将两个参数放在一个字符串中,然后再将该字符串放在列表中。在某些情况下,它会起作用,但这不是你可以依赖的
在某些情况下,您可以使用带shell的列表,*或不带shell的字符串,**但通常情况下,除非您知道自己在做什么,否则您不应该这样做,而且在任何情况下,您仍然不应该使用包含一个字符串的列表,除非您有特定的原因需要这样做***
如果要使用参数列表,请执行以下操作:
subprocess.Popen(['./script.sh', variable], shell=False, …)
请注意,这是两个字符串的列表,而不是一个连接字符串的列表,并且shell=False
如果要使用shell命令行,请不要将命令行放在列表中,不要跳过参数之间的空格,并引用任何非静态参数,如下所示:
subprocess.Popen('./script.sh ' + shlex.quote(variable), shell=True, …)
*在Windows上使用带外壳的列表永远不会有用;它们只是以某种未指明的方式结合在一起。但是在Unix上,
子进程
将有效地将'/bin/sh'
和'-c'
预先添加到列表中,并将其用作/bin/sh
的arg列表,这可能比尝试引用shell参数更简单,至少比使用shell=False
显式调用/bin/sh
更简洁
**在Unix上使用不带shell的字符串永远不会有用;这只是试图找到一个名称为整个字符串的程序,它将失败(除非你真的很不走运)。但在Windows上,它可能很有用subprocess
尝试将参数组合成一个字符串,传递给CreateProcess
,以便MSVCRT将它们解析回另一侧的相同参数列表,在某些边缘情况下,需要自己创建该字符串
***基本上,您希望准确地生成
['/bin/sh','-c',]
。您将两种不同的方式结合起来。最重要的是,你做错了,但仅仅纠正“做错了”并不是答案
您可以将两个参数放在一个列表中,然后在不使用shell的情况下启动它,如['./script.sh',variable]
。这通常更好。乌辛