expect fails when running proc inside proc expect fails when running proc inside proc unix unix

expect fails when running proc inside proc


I don't have my copy of "Exploring Expect" handy, but I think you're running into a variable scoping issue. spawn invisibly sets a variable named spawn_id. When you call spawn in a proc, that variable is scoped only for that proc. Declare it as global:

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

I think you don't have to do the same thing in sftp_send_password because expect has a more forgiving scoping scheme than Tcl (if expect does not find a local variable, look in the global namespace).

Your sftp_send_password proc will not affect the times variable in sftp_connect though, due to the same variable scoping issue. I'd recommend

proc sftp_send_password {times_var} {  upvar 1 $times_var times     ;# link this var to that in the caller  send "${SFTP_PASSWORD}\n";  expect {    "sftp>" { puts "Connected"; incr times; exp_continue}   }  # note use of `incr` instead of `expr`}

And then the sftp_connect proc sends the times variable name:

sftp_send_password times


The following is from the expect's man page:

Expect takes a rather liberal view of scoping. In particular, variables read by commands specific to the Expect program will be sought first from the local scope, and if not found, in the global scope. For example, this obviates the need to place global timeout in every procedure you write that uses expect. On the other hand, variables written are always in the local scope (unless a global command has been issued). The most common problem this causes is when spawn is executed in a procedure. Outside the procedure, spawn_id no longer exists, so the spawned process is no longer accessible simply because of scoping. Add a global spawn_id to such a procedure.