我会重写您的代码以删除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
都必须是非贪婪的。