Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Expect spawn command inside Tcl proc

Status
Not open for further replies.

gyoungmssg

Programmer
Feb 27, 2009
1
US
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top