Results 1 to 3 of 3
I'm having problems getting expect to run for simple prompt automation so i'm writing a test script to figure out where my problem is without it plugging into the larger ...
- 05-02-2011 #1Just Joined!
- Join Date
- Apr 2011
- Posts
- 9
Still having expect problems
I'm having problems getting expect to run for simple prompt automation so i'm writing a test script to figure out where my problem is without it plugging into the larger program. Included are my test script and my expect script. Both are stored in the same directory.
ptest
---------------------------------------
#!/bin/bash
INPUT="n"
echo "y"
read INPUT
if [ "$INPUT" == "y" ]; then
echo "It worked!"
else
echo "It did not work"
echo "$INPUT"
fi
----------------------------------------
then my expect script is:
---------------------------------------------------
#!/usr/bin/expect
spawn ptest
expect "y"
send "y\n"
------------------------------------------
When i run the expect script i get the error:
couldn't execute "ptest": no such file or directory
while executing
"spawn ptest"
(file "./doittmp" line 2)
I dont understand this as i can run ptest by itself and it works fine, but for some reason i cant spawn it from within the expect script. Any ideas?
- 05-03-2011 #2Just Joined!
- Join Date
- Apr 2011
- Posts
- 9
quick tutorial for anyone searching
Okay, i think i've finally got my expect demons exorcised. This reply is for anyone who happens to be searching and comes across it. A quick tutorial on expect to save anyone else the headaches i went through. First, install inspect... yum install expect works nicely.
1) include #!/usr/bin/expect on the top line (path may vary, I've also seen it listed as #!/usr/local/bin/expect)
2)spawn ./(path to file you want to spawn) EX: spawn ./testfolder/test (this starts the program that you want to automate)
3)expect "(what you want to respond to)"
4)send " (what you want to reply with)\r" #\r is used for designating return. Don't use carriage return or line feed as those come from automated end of lines, not interactive end of lines, and will not work.
5)end your script with "interact" this tells the script when to hand control back over. If you don't include this it will spawn the process, do nothing, and immediately kill it.
Alternately, if you need to expect several possibilities and provide an answer depending on which one comes up you can replace lines 3 and 4 with something like this:
expect "hi" { send "1\r"} \
"hello" { send "2\r" } \
"howdy" { send "3\r" }
It follows basically the same logic as a case or switch statement. If hi is found it will reply 1, if hello comes up it will reply 2, and if howdy comes up, it will reply 3. It will search for them simultaneously.
Also, you only need to worry about the carriage returns and line feeds if they fall in the middle of your expect condition, meaning you are expecting more than one line. If you do need them, they are designated by \r\n
Finally, I have only found one good source of documentation for expect and that is "Explore expect" it is available both from amazon and in pdf form.
PS: I know these aren't the "expect" forums, but i didnt know where else to ask.Last edited by vistal; 05-03-2011 at 02:17 AM. Reason: typo
- 05-03-2011 #3Linux Engineer
- Join Date
- Apr 2006
- Location
- Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
- Posts
- 1,117
Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
We look forward to helping you with the challenge of the other 10%.
( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )


Reply With Quote