Consider the following script, mostly copied for the book Exploring Expect
#!/usr/bin/expect
log_user 0
proc log_by_tracing {array element op} {
uplevel {
global logfile
set file $logfile($expect_out(spawn_id))
puts -nonewline $file $expect_out(buffer)
}
}
trace variable expect_out(buffer) w log_by_tracing
spawn bash
set logfile($spawn_id) [open "foobar.output" w]
expect -re "\$ $"
send "echo 0r"
expect -re "\$ $"
send "echo yr"
expect -re "\$ $"
send "echo foo; exitr"
expect "*"
wait
close $logfile($spawn_id)
The output of this script is the following:
$ echo 0
0
$ echo y
y
$
Observe that it is missing echo foo; exit
, as I would have expected.
Why does this happen, and is it possible to get the complete session using this variable tracing method?
Note that I deliberately do not use log_user
because I want to interact with multiple processes and get their output in separate files. I’m using a single process here just for clarity of exposition.