我的代码无限运行而不会出现循环。我从shell脚本调用expect脚本,工作正常,这里的问题是脚本不是来自timout {}循环。有人可以在这方面帮助我。
spawn ssh ${USER}@${MACHINE}
set timeout 10
expect "Password: "
send -s "${PASS}\r"
expect $prompt
send "cmd\r"
expect $prompt
send "cmd1\r"
expect $prompt
send "cmd2\r"
expect $prompt
send "cmd3\r"
expect $prompt
send "cmdn\r"
#cmdn --> is about running script which takes around 4 hours
expect {
timeout { puts "Running ....." #<--- script is nout coming out of loop its running infinitely
exp_continue }
eof {puts "EOF occured"; exit 1}
"\$.*>" { puts "Finished.." ; exit 0}
}
问题在于你的真实模式"\$.*>"
是字面匹配而不是正则表达式。你需要传递该模式的-re
标志作为RE匹配,就像这样(我使用了比;
字符更多的行,因为我觉得它更清晰,但是YMMV那里):
expect {
timeout {
puts "Running ....."
exp_continue
}
eof {
puts "EOF occured"
exit 1
}
-re {\$.*>} {
puts "Finished.."
exit 0
}
}
如果可以的话,将正则表达式放在{
braces}
中也是一个非常好的主意,因此内部的反斜杠序列(和其他Tcl元字符)不会被替换。你不必......但99.99%的情况都是这样的。