Not waiting for user input while waiting for script

When I try to run the next pending script, it just quits, instead it waits for user input. Can someone tell me what I am doing wrong?

#!/usr/bin/expect
puts -nonewline stdout "Enter device id:"
flush stdout
gets stdin id
puts -nonewline  stdout "Enter device name:"
flush stdout
gets stdin name
+5
source share
2 answers

Waiting modifies the Tcl command getsso that it does not wait for standard input; to read a line while waiting for it, you need to do this instead gets stdin id:

# Read input to stdin
expect_user -re "(.*)\n"
set id $expect_out(1,string)
+6
source

Try this code:

expect "\\$"
puts -nonewline "please enter a swithch user:  "
flush stdout
set userName [gets stdin]
puts $userName
expect "\\$" ;# without this line, the script would exit too fast 
             ;# for the "echo hello" to be sent to stdout by bash
             ;# and thus wont be recorded
0
source

All Articles