Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
asking for expect which used to interact program?

Posted by lanjun (lanjun), 2 November 2006
hi,
I want to use a script file to control a c program. The file is flowing:

test.exp file:

#!/usr/bin/expect
set timeout 1
log_file "test.txt"
spawn test

#send "123\r"

#the paragram which needed by test program is stored in test1.txt file.
set output [open test1.txt r]
set f [read $output]
send $f
close $output

#send "75,63,98\r"
expect eof


test.c file:
#include <stdio.h>
main()
{
 int a,b,c,d;
 do
   {
     printf ("input a :\n");
     scanf ("%d",&a);
     printf ("%d\n",a);
   }while (123!=a);
 printf ("Please input b,c,d:\n");
 scanf ("%d,%d,%d", &b,&c,&d);
 printf("%d,%d,%d\n",b,c,d);
}

text1.txt file:
123
78,50,96


ok,lets run it.and get the result:
spawn test
123
78,50,96input a:
123
Please input b,c,d:


As you know, i expect to get this result:
input a:
123
Please input b,c,d:
78,50,96


why??why i can't get the right result i expectedit makes me felt very wondering. can you give me some wonderfull advice for that.
thank you very much!!!!




Posted by admin (Graham Ellis), 2 November 2006
When you input the value to the C program, it echos back the input you made and you need to suck that up with your expect program.

Posted by lanjun (lanjun), 3 November 2006
thanks for your kindness to reply my stupid question.
"suck that up with your expect program", i'm sorry i cann't understand it very clearly. do you mean i must write something to get the c program's return value?
en, let me see...is it possible? beacuse i cann't find one exppect command for that function. and do you have a good idea?

Posted by admin (Graham Ellis), 3 November 2006
Your expect must also read back your inputs which are echoed by the server.  In  addition, you may like to clean up your display by setting log_user to 0.  

Posted by lanjun (lanjun), 3 November 2006
well, yeah, you are right. send inputs must be twice. so i get the right return value... ; expect language is so wondderful.  i hope i can control it one day...hehe,that heared a little long..
thanks,Graham Ellis!!!



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho