Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pipe to terminal and to program at the same time in bash

Tags:

c

bash

So I am attempting to make a script to automate the testing(in and out) of my C program and I am trying to get the input to stay on screen so I know what is happening.

I have only tried piping so far:

foo < input.txt

and

cat input.txt | tee dev/tty |foo

which has yet to work for me.

So assuming the input file would look like:

123
321

Ideally, the IO would look like:

Input: 123
Echo: 123
Input: 321
Echo: 321

but it turns into

123
321
Input: 
Echo: 123
Input: 
Echo: 321

Are there other methods that I can use to test my C program? Where else can I tee to that can achieve such result? Is it possible for me to write another C program that can achieve something similar?

like image 504
Zheng Chen Avatar asked Nov 08 '22 07:11

Zheng Chen


1 Answers

The stdout of tee (to stdin of foo) and the duplicated writes (to your tty character dev) are not synchronized in time. Your tty is consuming input faster than foo and the buffering of libc is making it even worse. If interactive automation is what you seek, look to the expect program.

like image 159
nabin-info Avatar answered Nov 15 '22 05:11

nabin-info