Let's say I have this program math that generates and prints a random math question and than waits for an input.

How would I create a script to receive the input, process it, and then print it back to the original program?

I've tried to do something like this ./math | script.sh | ./math but the second program generates a different problem, so it doesn't work.


1 Answer 1


Without any sample output or behavior, it's hard to provide an exact response, but it seems you're looking for the expect command.

Manpage expect(1):

EXPECT(1)                   General Commands Manual                  EXPECT(1)

       expect - programmed dialogue with interactive programs, Version 5

       expect [ -dDinN ] [ -c cmds ] [ [ -[f|b] ] cmdfile ] [ args ]

       Expect  is a program that "talks" to other interactive programs accord‐
       ing to a script.  Following  the  script,  Expect  knows  what  can  be
       expected  from  a  program and what the correct response should be.  An
       interpreted language provides branching and high-level  control  struc‐
       tures  to  direct the dialogue.  In addition, the user can take control
       and interact directly when desired, afterward returning control to  the

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .