如何在运行Shell脚本时在python的子过程调用中传递多个参数?
import subprocess
subprocess.check_call('./test_bash.sh '+arg1 +arg2, shell=True)
这会打印出作为一个参数串联的arg1和arg2。我需要将3个参数传递给我的shell脚本。
当然,它是串联的,因为您没有在它们之间插入空格。Quickfix将是(带有format
,如果某些参数包含空格,则可能会失败)
subprocess.check_call('./test_bash.sh {} {}'.format(arg1,arg2), shell=True)
您可以尝试吗(更强大,无需引用空格,自动生成命令行):
check_call(['./test_bash.sh',arg1,arg2],shell=True)`
(由于shell=True
和参数列表一起使用,因此不确定它是否在所有系统上都有效)
或shell=True
显式地删除并调用该外壳程序(可能会失败,因为不考虑使用shebang,这与不同shell=True
,但是值得放弃,因为它有shell=True
可能注入代码以及其他问题):
check_call(['sh','-c','./test_bash.sh',arg1,arg2])
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句