gets not waiting for user input in expect script
Asked Answered
L

2

5

When i try to run the following expect script, it just finishes running instead waiting for user input. Could 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
Lemar answered 30/4, 2012 at 15:14 Comment(0)
B
9

Expect alters the Tcl gets command so that it doesn't wait for standard input; to read a line while waiting for it, you need to do this instead of gets stdin id:

# Read input to stdin
expect_user -re "(.*)\n"
set id $expect_out(1,string)
Blader answered 30/4, 2012 at 20:56 Comment(1)
Notes: I found how to get input from the user in the expect documentation, and I confirmed that Expect changes the blocking behavior of gets stdin by testing. I was very surprised by that change.Blader
C
0

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
Cologne answered 16/6, 2015 at 0:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.