我有一个shell脚本,它询问用户太多问题.
我想回答以-结尾的每个问题,以-结尾的每个问题?与日元.
例如.,
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)
如何查询脚本的输出,等待问题出现?
解决方法:
尝试这样的事情(我还没有测试过):
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