我有一个shell脚本,询问用户太多问题。
我想回答每个以:
结尾并带有输入的问题,以及以?
结尾y
结尾的每个问题
如,
Enter your name: enter Enter your email: enter ... Are you sure these details are correct? yenter
我用:
启动了子进程subprocess.Popen(shell=True, stdout=sp.PIPE, stderr=sp.STDOUT, stdin=sp.PIPE)
如何轮询脚本的输出,等待问题出现?
答案 0 :(得分:4)
尝试这样的事情(我还没有测试过):
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