2017-02-24 50 views
0

我有一個期望命令尋找一個字期待輸出

expect "~]#" { send "virsh list --all\r"} 

和輸出將

[[email protected] ~]# virsh list --all 
Id Name       State 
---------------------------------------------------- 
399 lht1duplexvm-0     running 
-  rhelvm       shut off 

我想用$ expect_out(緩衝),並有一個if語句來完成如果它發現運行,並且如果不是,則執行其他操作。 我該如何解析$ expect_out(buffer)的結果?

回答

1
expect "~]#" 
send "virsh list --all\r" 

# I assume another prompt follows this 
expect "~]#" 

if { [regexp {running} $expect_out(buffer)] } { 
    do-something-for-running-process 
} else { 
    do-something-for-no-running-process 
} 

你也可以做

if {[string first "running" $expect_out(buffer)] >= 0} {