bash - expect fails when running proc inside proc -


my script works fine (retrieves sftp prompt) when using 1 proc. when try use proc inside proc, script gets stuck, , not know why.

please not refactor code, not point, need understand issue here.

working code:

proc sftp_connect {} {   set times 0;   set connection_retry 2   set timeout 1;   while { $times < $connection_retry } {     spawn sftp ${sftp_user}@${sftp_server}     expect {       timeout { puts "connection timeout"; exit 1}       default {exit 2}       "*assword:*" {          send "${sftp_password}\n";         expect {           "sftp>" { puts "connected"; set times [ expr $times+1]; exp_continue}         }       }     }   }   send "quit\r"; }  sftp_connect 

debug output:

expect: "\r\nsftp> " (spawn_id exp5) match glob pattern "sftp>"? yes 

but after moving send password separate proc, expect not retrieve sftp prompt anymore ("sftp>"):

proc sftp_send_password {} {   send "${sftp_password}\n";   expect {     "sftp>" { puts "connected"; set times [ expr $times+1]; exp_continue}   } }  proc sftp_connect {} {   set times 0;   set connection_retry 2   set timeout 1;   while { $times < $connection_retry } {     spawn sftp ${sftp_user}@${sftp_server}     expect {       timeout { puts "connection timeout"; exit 1}       default {exit 2}       "*assword:*" { sftp_send_password }     }   }   send "quit\r"; }  sftp_connect 

debug output:

expect: "" (spawn_id exp0) match glob pattern "sftp>"? yes 

i don't have copy of "exploring expect" handy, think you're running variable scoping issue. spawn invisibly sets variable named spawn_id. when call spawn in proc, variable scoped proc. declare global:

proc sftp_connect {} {   global spawn_id   # ... rest same } 

i think don't have same thing in sftp_send_password because expect has more forgiving scoping scheme tcl (if expect not find local variable, in global namespace).

your sftp_send_password proc not affect times variable in sftp_connect though, due same variable scoping issue. i'd recommend

proc sftp_send_password {times_var} {   upvar 1 $times_var times     ;# link var in caller   send "${sftp_password}\n";   expect {     "sftp>" { puts "connected"; incr times; exp_continue}    }   # note use of `incr` instead of `expr` } 

and sftp_connect proc sends times variable name:

sftp_send_password times 

Comments

Popular posts from this blog

Java 3D LWJGL collision -

methods - python can't use function in submodule -

c# - ErrorThe type or namespace name 'AxWMPLib' could not be found (are you missing a using directive or an assembly reference?) -