简体   繁体   English

使用python脚本打开Git Bash shell,然后在git bash shell中运行shell脚本

[英]Open Git Bash shell using a python script and then run a shell script in git bash shell

I am trying to open Git Bash shell(C:\\Program Files\\Git\\git-bash.exe) using a python script and then run a shell script(for example, basicSc.sh) in git bash shell. 我正在尝试使用python脚本打开Git Bash shell(C:\\ Program Files \\ Git \\ git-bash.exe),然后在git bash shell中运行shell脚本(例如,basicSc.sh)。 I am running the python script in Windows. 我在Windows中运行python脚本。 How can I do that successfully? 我怎样才能成功做到这一点? Also how can I know the *.sh script is completed from the python script? 另外我怎么知道* .sh脚本是从python脚本完成的呢? How can I pass some parameters for the *.sh script from the python script? 如何从python脚本传递* .sh脚本的一些参数? I am running python 3.6.3. 我正在运行python 3.6.3。

Python script (running from windows command prompt): 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", 
)

Alternatively, if I do the following I can run the *.sh script manually. 另外,如果执行以下操作,则可以手动运行* .sh脚本。 I am trying to fully automate this. 我正在尝试使其完全自动化。 If this is the only possible solution, how do I communicate end of the *.sh script to the python script? 如果这是唯一可能的解决方案,那么如何将* .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", 
)

Reason Popen accepts list of arguments so it should look like as follows 原因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", 
                     )

使用p.wait()来确保您的脚本成功运行.....但是,其他方法可以将脚本的所有进度捕获到任何记录器文件中,然后检查其是否在执行应做的工作。 .....您也可以使用CHECK_OUTPUT,因为它返回返回代码,可用于断言脚本是否成功运行。...请在Try-Except catch中保持此状态以捕获异常

I was able to automate the process by doing the following. 通过执行以下操作,我能够使该过程自动化。 The shell script is in "C:/Users/userName/Documents/dev" directory 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", 
                 )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM