loukya
loukya

Reputation: 11

how to run expect script on a given spawn id

How to run bunch of expect scripts on spawnid from first expect script

Here are the details: I have first expect script that shall spawn a application called openapp. Now i want to run tests(written in expect scripts - abc), that will do different validations on the spawned application id

#!/usr/bin/expect -f
source home/tests/library.tcl
set openapp [runapp]
eval spawn $openapp
set id $spawn_id
login
puts [exec home/tests/testsuite/abc $id]
expect eof

#!/usr/bin/expect -f
sourcehome/tests/library.tcl
puts 2ndscript
set bbb [lindex $argv 0]
puts $bbb
send -i $bbb "t\r"
expect eof


Here are results i get:

exp5 2ndscript exp5 can not find channel named "exp5" while executing "send -i $bbb "t\r"" (file "/home/tests/testsuite/abc" line 6) while executing "exec home/tests/testsuite/abc $id" invoked from within "puts [exec home/tests/testsuite/abc $id]" (file "/home/tests/testsuites/xyz" line 7)

I am not able to run expect commands on spawnid from other script

Upvotes: 1

Views: 1595

Answers (1)

Donal Fellows
Donal Fellows

Reputation: 137567

Spawn IDs are entirely local to a particular script; they can't usefully be sent even to another thread in the same process (excepting for sending them back to the original script).

You need to write your code in your second file as a procedure that takes the spawn ID as an argument.

proc do_the_thing {id} {
    send -i $id "t\r"
}

then you can source the procedure from the first and have it do what you want:

#!/usr/bin/expect -f

source home/tests/library.tcl
eval spawn [runapp]
login
source secondscript.tcl
do_the_thing $spawn_id
expect eof

If you want your other script to also be callable independently, you can:

#!/usr/bin/expect -f

proc do_the_thing {id} {
    send -i $id "t\r"
}

# This is how you check if this script is the main script
if {$::argv0 eq [info script]} {
    # This is main; do our thing...
    source home/tests/library.tcl
    set bbb [spawn ...]
    puts $bbb
    do_the_thing $bbb
    expect eof
}

Upvotes: 1

Related Questions