我正在運行Solaris 5-10,python 2.6.2和pexpect 2.4爲什麼不正確地使用shell的輸出?
我有一個非常簡單的python腳本,在其中練習了從shell發送和接收文本的功能。
我的理解是,pexepect([pexpect.TIMEOUT,X,Y,Z],超時= 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
================================================================================
爲什麼我看到的「垃圾0」一直在重複輸出?它應該被第一個myShell.expect()語句使用,但它一直顯示。爲什麼??