Talking to programs

Dan Bernstein brnstnd at kramden.acf.nyu.edu
Mon Aug 20 00:09:06 AEST 1990


In article <1990Aug3.213540.14476 at Neon.Stanford.EDU> jonas at Neon.Stanford.EDU (jonas karlsson) writes:
> How do  i call a program from a function, have the function respond to 
> the various prompts, give the correct replies, and catch the output?

You have two problems. One is that most programs want to talk directly
to a terminal, not to a pipe or file or socket. To solve this problem,
you could pick up my pty program (ftp site down for maintenance; ask me
for a copy of the c.s.unix submission). ``pty foo'' handles redirection
where ``foo'' cannot.

The other is that pipes are one-way, while you want two-way
communication. If you have named pipes, here's one easy solution:

  (umask 077;mknod input p;mknod output p)   # on this Sun, anyway
  pty program args < input | tee record > output
  # now read prompts from output and write replies into input
  # record contains a transcript

Otherwise you'll have to write some code to stick normal pipes in front
of and in back of the program. You can skip this coding by picking up
``expect'' from Don Libes; it also does the necessary pseudo-terminal
work, though pty is more flexible. ``expect'' is available via anonymous
ftp to 129.6.32.4, if I remember right.

---Dan



More information about the Comp.unix.wizards mailing list