[英]Python subprocess: interacting with a shell script
I have a shell script which asks the user for too many questions. 我有一个shell脚本,询问用户太多问题。
I want to answer every question that ends with :
with a enter , and every question that ends with a ?
我想回答每一个结尾的问题:
输入 ,每个问题以?
结尾?
with y
enter . 与y
输入 。
eg, 例如,
Enter your name: enter Enter your email: enter ... Are you sure these details are correct? yenter
I have started the subprocess with: 我已经启动了子进程:
subprocess.Popen(shell=True, stdout=sp.PIPE, stderr=sp.STDOUT, stdin=sp.PIPE)
How do I poll over the script's output, waiting for the question to appear? 如何轮询脚本的输出,等待问题出现?
Try something like this (I have not tested it): 尝试这样的事情(我还没有测试过):
import pexpect
child = pexpect.spawn('yourprogram')
while True:
found = child.expect ([r':$', r'\?$', pexpect.EOF])
if found == 0:
child.send('\n')
elif found == 1:
child.send('y\n')
else: # EOF
return
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.