2013-06-12 25 views
1

我正在尋找某種功能來在標準輸出中「偷看」,而不從預期的緩衝區中移除它,以便可以通過另一個期望命令讀取它。或者,有沒有辦法在閱讀後將它放回緩衝區?有沒有一種方法可以預期輸出而不將其從Tcl中的緩衝區中移出呢?

編輯:有人問了一些代碼,它基本上是一個shell提示符類似於:

(prompt) bad_command_that_was_sent 
error message 
(prompt) successful_command_that_was_sent 
(prompt) other_successful_command 
long barf of data that has 
very little consistency 
and almost no way to tell when it\'s 
about to end as the prompt just shows 
up again suddenly but I really want to save 
it and parse it. 
(prompt) 

而現在,我看它是這樣的:

expect { 
    -re "Error message regex" {error handling part} 
    -re "Prompt regex" {anything I need to do with successes} 
} 

我目前使用一個解決方法,我發送一個額外的換行符(send "command\r\r"),它讓我2提示檢測,但這不是理想的,並且實際上已經導致一些錯誤。

+1

你能發表一些代碼嗎? – pn8830

回答

1

如果你想捕獲所有的命令輸出,不包括提示:

set prompt_re {\(prompt\) $} 
send -- "other_successful_command\r" 
expect { 
    -re $err_re {handle error} 
    -re "(.+)$prompt_re" { 
     doSomethingWith $expect_out(1,string) 
    } 
} 

如果你希望一噸的數據,看看到match_max命令。


所以你不知道什麼時候出現錯誤。我假定遠程系統是一個Bourne類型的shell:執行命令,捕獲輸出,查詢退出狀態,然後判斷命令的成功/失敗。

send -- "some command\r" 
expect { 
    -re "(.+)$prompt_re" { 
     set commandOutput $expect_out(1,string) 
     send "echo \$?\r" 
     expect -re "(\\d+)\\s+$prompt_re" 
     set exitStatus $expect_out(1,string) 
     doSomethingWith $exit_status $command_output 
    } 
} 
+0

問題是沒有什麼我可以匹配$ err_re。只有一個提示,然後這個提示仍然被這個expect命令捕獲,並且不會被未來的提示所捕獲。 –

相關問題