gyoungmssg
Programmer
I'm new to Tcl/Expect and this was not apparent to me so I thought it might help others...
#!/usr/bin/expect
#
# This an expect script that demonstrates a spawn command in one proc with the
# expect command in a different proc. The secret sauce here is the spawn_id
# must be defined in each proc with the global command. In general, the spawn
# command and the expect command must use the same spawn_id or your script will
# appear to hang in the expect processing.
#
proc do_spawn {} {
global spawn_id
set pid [spawn ls -al]
return $pid
}
proc do_expect {} {
global spawn_id
global filename
set timeout 5
expect {
$filename {
send_user "+++ Found filename \"$filename\" in the current directory.\n"
return
}
timeout {
return "expect command timedout after 5 seconds.\n"
}
eof {
return "+++ Did not find file \"$filename\" in the current directory.\n"
}
}
}
# This script tries to find a file in the current directory.
if {$argc == 0} {
send_user "You must enter a filename.\n"
exit
}
set filename [lindex $argv 0]
set ret_spawn [do_spawn]
if {$ret_spawn == 0} {
send_user "spawn ls -al failed\n"
}
send_user [do_expect]
exit
#!/usr/bin/expect
#
# This an expect script that demonstrates a spawn command in one proc with the
# expect command in a different proc. The secret sauce here is the spawn_id
# must be defined in each proc with the global command. In general, the spawn
# command and the expect command must use the same spawn_id or your script will
# appear to hang in the expect processing.
#
proc do_spawn {} {
global spawn_id
set pid [spawn ls -al]
return $pid
}
proc do_expect {} {
global spawn_id
global filename
set timeout 5
expect {
$filename {
send_user "+++ Found filename \"$filename\" in the current directory.\n"
return
}
timeout {
return "expect command timedout after 5 seconds.\n"
}
eof {
return "+++ Did not find file \"$filename\" in the current directory.\n"
}
}
}
# This script tries to find a file in the current directory.
if {$argc == 0} {
send_user "You must enter a filename.\n"
exit
}
set filename [lindex $argv 0]
set ret_spawn [do_spawn]
if {$ret_spawn == 0} {
send_user "spawn ls -al failed\n"
}
send_user [do_expect]
exit