samvel1024
samvel1024

Reputation: 1163

Redirect the output of a process to itself as an input during execution using bash

I have a program which reads from stdin and prints to stdout. I want to be able to redirect output to serve as an input for the program during its execution. I prefer not to use expect. Below is an example script using fifo but it's flawed in some way which I am unable to understand.

mkfifo fifo
echo "Initial input" > fifo
cat  fifo |  ./my_program > fifo
rm -rf fifo

Upvotes: 0

Views: 64

Answers (1)

that other guy
that other guy

Reputation: 123410

This fails because the echo statement will wait forever for a reader to attach.

One alternative is:

mkfifo fifo
{ echo "Initial input"; cat fifo; } |  ./my_program > fifo

It's up to ./my_program to:

  • Flush its output buffer
  • Not exceed the pipe buffers
  • Exit at a reachable condition

If the program fails to do any of the above, it will hang or deadlock. This is a problem with ./my_program and not with the bash snippet.

Upvotes: 1

Related Questions