tcl - All expect statements across procedures in a script pattern match on output produced by the only spawned process in a particular procedure -


consider scenario of sort:

    #start of script     proc {host_ip} {         spawn ssh $host_ip         #possibly 1 n expect statements pattern          #match on output of spawned process     }      proc b {} {        #pattern match using expect statements on         #output of spawned process in     }      proc c {} {        #pattern match using expect statements on         #output of spawned process in     }     #call proc's     $some_ip     b     c     #pattern match here, not part of procedure,     #but expect statement match output of      #the spawned process in      #end of script 

from reading expect/tcl online documentation appears have 2 options:

  1. return pid of spawned process in a, , explicitly use in expect statements outside of using expect statement form:

    expect -i $pid_from_a ...  
  2. there appears magic/global variable if value set pid of spawned process in a, expect statements in script can pattern match on output of spawned process in a.

1 work, have not tested, documentation clear on this. prefer 2 because dont want litter script explicitly passing pid each expect statement, dont know global variable override (if there one). appreciated. thanks.

the magic variable called spawn_id. have put global spawn_id inside procedure calls spawn (before spawn), otherwise can leave undeclared. because when expect reads variable, searches more current scope, when writes variable writes current scope (this not normal tcl behaviour; tcl reads , writes current scope unless explicitly told otherwise). expect manual:

expect takes rather liberal view of scoping. in particular, variables read commands specific expect program sought first local scope, , if not found, in global scope. example, obviates need place "global timeout" in every procedure write uses expect. on other hand, variables written in local scope (unless "global" command has been issued). common problem causes when spawn executed in procedure. outside procedure, spawn_id no longer exists, spawned process no longer accessible because of scoping. add "global spawn_id" such procedure.

thus, need change a this:

proc {host_ip} {     global spawn_id     spawn ssh $host_ip     #possibly 1 n expect statements pattern      #match on output of spawned process } 

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -