Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Input to executables

Status
Not open for further replies.

gonzilla

Programmer
Apr 10, 2001
125
US
Hi,

I have an executable that takes user input in the form of things like 'y' and 1 or maybe a string of text occasionally. I would like to write a UNIX script that calls this program - which is no problem. But, I would like to have this script provide the input the program needs as well. I'm not talking about args on the command line, I mean there are actual reads from the program.

I have tried to echo the responses in reverse order and pipe it to the executable...

(echo y;echo 1) | execprog

I have seen that work on other programs but it doesn't seem to work in this case. I know for sure the executable is a C program - although I didn't write it nor do I have the source code.

Any help would be great. Thanks.

-Tyler
 
Tyler:

Instead of piping, I'd put the keystrokes into a file, one string per line; call it keystrokes.dat:

echo y
echo 1

and then use redirect that file as the input:

execprog < keystrokes.dat

You may to experiment to get it right.

Regards,


Ed
 
Take a look at a great utility called expect, it will enter information in a console app as required :)

It is something I always like to have in my toolbox !

--d3vNull
 
Thanks for your guys help with this.

d3vNull - where can I find expect? It isn't in the man pages. Is this a utility that isn't normally installed?

-Tyler
 
Tyler,

Try out something like this

export str=&quot;1&quot;
export filename=&quot;xx&quot;
echo $str &quot;is value of string&quot;
echo $filename &quot;is value of filename&quot;
grep $str $filename

The script set the value of variables str and filename, you can replace it with your own script variables. Then it prints out the variables just for debugging purposes.It then invokes the command grep(which is a C-program) with the two variables as arguments.
Create a file called xx before running the script.

cheers
amit
crazy_indian@lycos.com

to bug is human to debug devine
 
expect is a third paty program built on top of TCL/TK and as the other poster has said is invaluable, you can download it from the net. In addition there is a good O'Reilly book on the program called Exploring Expect.
 
The simpler way to send all input needed by a program from a shell is:
Code:
program <<EOF
y
1
EOF

The program will act as if all lines between <<EOF and EOF are just type to the keyboard.

In fact, the shell creates a temporary file, put all lines between <<EOF and EOF in this file and redirects the standard input of the program to read in this file instead of the keyboard.

 
Awesome! Thanks for everybody's help - I really appreciate it.

I'll give them all a try and see which is best for this particular situation.

-Tyler
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top