2012-04-30 86 views
5

當我嘗試運行以下期望腳本時,它只是完成運行,而不是等待用戶輸入。有人能告訴我我做錯了什麼嗎?獲取不等待用戶輸入的期望腳本

#!/usr/bin/expect 
puts -nonewline stdout "Enter device id:" 
flush stdout 
gets stdin id 
puts -nonewline stdout "Enter device name:" 
flush stdout 
gets stdin name 

回答

6

期望改變Tcl gets命令,以便它不等待標準輸入;讀取線,而等待它,你需要做的,而不是gets stdin id

# Read input to stdin 
expect_user -re "(.*)\n" 
set id $expect_out(1,string) 
+1

注:我發現如何從用戶的預期文檔中獲取輸入,並且我確認了Expect通過測試改變了'獲取stdin'的阻塞行爲。我非常驚訝於這種變化。 –

0

試試這個代碼:

expect "\\$" 
puts -nonewline "please enter a swithch user: " 
flush stdout 
set userName [gets stdin] 
puts $userName 
expect "\\$" ;# without this line, the script would exit too fast 
      ;# for the "echo hello" to be sent to stdout by bash 
      ;# and thus wont be recorded