在期望输出中搜索单词

时间:2017-02-24 15:47:28

标签: linux parsing output expect

我有一个期望命令

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

,输出为

[root@lht1oneems-unit0 ~]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 399   lht1duplexvm-0                 running
 -     rhelvm                         shut off

我想使用$ expect_out(缓冲区)并且如果发现正在运行则使用if语句执行某些操作,否则执行其他操作。 如何解析$ expect_out(缓冲区)的结果?

1 个答案:

答案 0 :(得分: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} {