我正在尝试使用python脚本打开Git Bash shell(C:\ Program Files \ Git \ git-bash.exe),然后在git bash shell中运行shell脚本(例如,basicSc.sh)。我在Windows中运行python脚本。我怎么能成功地做到这一点?另外我怎么知道* .sh脚本是从python脚本完成的?如何从python脚本传递* .sh脚本的一些参数?我正在运行python 3.6.3。
Python脚本(从Windows命令提示符运行):
import subprocess
p = subprocess.Popen
(
"C:\Program Files\Git\git-bash.exe --l -- C:/Users/userName/Documents/dev/basicSc.sh",
bufsize=-1,
executable=None,
stdin=None,
stdout=None,
stderr=None,
preexec_fn=None,
close_fds=True,
shell=False,
cwd="C:/Users/userName/Documents/dev",
)
或者,如果我执行以下操作,我可以手动运行* .sh脚本。我试图完全自动化这个。如果这是唯一可能的解决方案,我如何将* .sh脚本的结尾传递给python脚本?
import subprocess
p = subprocess.Popen("C:\Program Files\Git\git-bash.exe",
bufsize=-1,
executable=None,
stdin=None,
stdout=None,
stderr=None,
preexec_fn=None,
close_fds=True,
shell=False,
cwd="C:/Users/userName/Documents/dev",
)
答案 0 :(得分:1)
原因Popen接受参数列表,因此它应如下所示
import subprocess
p = subprocess.Popen(["C:\Program Files\Git\git-bash.exe","C:/Users/userName/Documents/dev/basicSc.sh"],
bufsize=-1,
executable=None,
stdin=None,
stdout=None,
stderr=None,
preexec_fn=None,
close_fds=True,
shell=False,
cwd="C:/Users/userName/Documents/dev",
)
答案 1 :(得分:1)
使用p.wait()确保您的脚本成功运行.....但是其他方法可以将脚本的所有进度捕获到任何记录器文件,然后检查它是否正在执行它应该执行的操作或不....... 您也可以使用CHECK_OUTPUT,因为它返回返回代码,可用于断言您的脚本是否成功运行....将此保留在Try-Except catch中以捕获异常
答案 2 :(得分:0)
通过执行以下操作,我可以使该过程自动化。 Shell脚本位于“ C:/ Users / userName / Documents / dev”目录中
import subprocess
p = subprocess.Popen("C:\Program Files\Git\git-bash.exe ./shellScriptName.sh
argumentsForShellScript",
bufsize=-1,
executable=None,
stdin=None,
stdout=None,
stderr=None,
preexec_fn=None,
close_fds=False,
shell=False,
cwd="C:/Users/userName/Documents/dev",
)