我重写代码删除while
循环:
set number_ok 0
set node_patt "(Reloading configuration on node (\\d+?) \\((\[^)]+?)\\))\r\n(.+?)\r\n"
send "cluster config -r -a\r"
expect {
ERROR {cluster_exit 1}
-re $node_patt {
set line "<$cmdline> $expect_out(1,string)"
set node_num $expect_out(2,string)
set node_name $expect_out(3,string)
set result $expect_out(4,string)
switch -regexp $result {
"Node .+ not found" {
ok 0 "$line (not found)"
}
"Node .+ not responding, skipped" {
ok 0 "$line (not responding)"
}
OK {
ok 1 $line
incr number_ok
}
}
exp_continue ;# loop back to top of expect block
}
$ROOT_PROMPT ;# no action, so fall out of the expect block
}
注意,Tcl的正则表达式要么完全贪婪或完全不贪婪。我使用\r\n(.+)\r\n
捕获“重新加载节点上的配置...”之后的行。但.+
部分不得包含换行符,因此它必须非贪婪。因此,node_patt
中的每个量词都必须是非贪婪的。
工作就像一个时钟!日Thnx! – Codeape 2010-09-22 06:52:23
请注意,Tcl的REs *不是*完全贪婪或不贪婪的,但是对于发生什么的规则是自动机理论的,除非在一些情况下非常难以理解(例如,先行约束,贪婪)。如果可能的话,只用一种类型的RE进行编写总是比较容易。 – 2010-09-22 12:58:50