Tcl 如何在给定的繁殖id上运行expect脚本

Tcl 如何在给定的繁殖id上运行expect脚本,tcl,expect,Tcl,Expect,如何在第一个expect脚本的spawnid上运行大量expect脚本 详情如下: 我有第一个expect脚本,它将生成一个名为openapp的应用程序。 现在我想运行测试(用expect scripts-abc编写),这些测试将对生成的应用程序id执行不同的验证 #!/usr/bin/expect -f source home/tests/library.tcl set openapp [runapp] eval spawn $openapp set id $spawn_id login pu

如何在第一个expect脚本的spawnid上运行大量expect脚本

详情如下: 我有第一个expect脚本,它将生成一个名为openapp的应用程序。 现在我想运行测试(用expect scripts-abc编写),这些测试将对生成的应用程序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

以下是我得到的结果:

表5 第二脚本 表5 找不到名为“exp5”的频道 执行时 发送-i$bbb“t\r” (文件“/home/tests/testsuite/abc”第6行) 执行时 “exec home/tests/testsuite/abc$id” 从内部调用 “放置[exec home/tests/testsuite/abc$id]” (文件“/home/tests/testsuites/xyz”第7行)


我无法在其他脚本的spawnid上运行expect命令,spawnid对特定脚本来说完全是本地的;在同一进程中,它们甚至不能有效地发送到另一个线程(将它们发送回原始脚本除外)

您需要在第二个文件中编写代码,作为以spawn ID作为参数的过程

proc do_the_thing {id} {
    send -i $id "t\r"
}
然后,您可以
source
从第一个开始执行该过程,并让它执行您想要的操作:

#!/usr/bin/expect -f

source home/tests/library.tcl
eval spawn [runapp]
login
source secondscript.tcl
do_the_thing $spawn_id
expect eof
如果希望其他脚本也可以独立调用,可以:

#!/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
}

expect eof
之后的第一个脚本中,生成的进程已经死了,因此无法继续与它交互。即使您没有
expect eof
,生成的进程也将在expect脚本完成时被杀死(通常通过
SIGHUP
)。您无法将spawnid从一个脚本传递到另一个脚本。感谢pynexj,因此在一个expect脚本中生成应用程序后,是否有其他方法调用其他expect脚本,而不是将spawnid从一个脚本传递到另一个脚本。所以基本上,我必须生成一个应用程序并登录,然后我想运行一些测试。因此,这些易于维护的测试必须在不同的文件中,而不是在我们生成应用程序的同一个文件中。非常感谢Donal。这正是我目前所做的,将其他测试调用为proc,然后在基类中对它们进行资源化。
#!/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
}