【发布时间】:2013-01-08 00:05:49
【问题描述】:
我正在运行 Solaris 5-10、python 2.6.2 和 pexpect 2.4
我有一个非常简单的 python 脚本,它在下面练习从 shell 发送和接收文本的功能。
我的理解是 pexepect([pexpect.TIMEOUT, x,y,z], timeout=w) 将返回自上次调用 pexpect 以来它找到的匹配的索引,但如果超过 w 秒,则返回 0。
这是我非常简单的脚本:
#!/usr/bin/env python
import pexpect
myPrompt = " % "
myShell = pexpect.spawn("/bin/tcsh")
print "Sending 'JUNK-0' to shell"
x = myShell.sendline("JUNK-0")
y = myShell.expect([pexpect.TIMEOUT], timeout=1)
print "y = %s" % y
print myShell.before
print "=" * 80
print "\n\n"
for i in range(2):
print "i = %d" % (i+1)
print "Sending 'JUNK-%d' to shell" % (i+1)
x = myShell.sendline("JUNK-%d" % (i+1))
y = myShell.expect([pexpect.TIMEOUT, myPrompt], timeout=10)
print "y = %s" % y
print myShell.before
print "=" * 80
print "\n\n"
仅供参考,我的 shell 提示符是“myMachine %”,但是在这个脚本中,我只是使用“%”来保持它的通用性。
当我运行它时,我看到以下输出:
Sending 'JUNK-0' to shell
y = 0
JUNK-0
myMachine % JUNK-0
JUNK-0: Command not found.
myMachine %
================================================================================
i = 1
Sending 'JUNK-1' to shell
y = 1
JUNK-0
myMachine
================================================================================
i = 2
Sending 'JUNK-2' to shell
y = 1
JUNK-0
JUNK-0: Command not found.
myMachine
================================================================================
为什么我看到“JUNK-0”始终在输出中重复出现?它应该被第一个 myShell.expect() 语句使用,但它一直出现。为什么??
【问题讨论】: