I read the following code, but I do not understand how it works:
set accum ""
set timeout 1
expect {
-re {.+} {
set accum "${accum}$expect_out(0,string)"
exp_continue
}
}
set timeout 10
at the beginning, we set accum and timeout, then there is a expect command try to match something? and after it, we set the timeout as 10, how the whole code works? and does this mean?
Until the code times out (1 second after the last match of anything), any time it matches something (which is any sequence of characters — possibly excluding newline — because of
-re {.+}) it appends it to theaccumvariable and restarts expecting something (theexp_continueis indeed magic).It would be more efficient to use
append accum $expect_out(0,string), but the way it is done isn’t wrong.