我正在尝试从Python中执行一个shell脚本(而不是命令):
main.py
-------
from subprocess import Popen
Process=Popen(['./childdir/execute.sh',str(var1),str(var2)],shell=True)
execute.sh
----------
echo $1 //does not print anything
echo $2 //does not print anything
var1和var2是我作为输入用于shell脚本的某些字符串。我是否漏掉了什么或是否有其他方法可以做到这一点?
shell=True
,但如果您使用它,则应使用shlex.quote()
转义var1
、var2
:output = check_output("./childdir/execute.sh " + " ".join(pipes.quote(str(v)) for v in [var1, var2]), shell=True)
。 - jfsbytes
,在使用之前可能需要转换为str
,例如:output.decode("utf-8")
。 - Gogowitsch