2017-02-24 85 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} {