bashstdinio-redirection

How to redirect to stdin in bash but with a delay between each line?


The platform I am working is Ubuntu 18.04. I have a binary program (so I cannot change the program) say foo and it reads commands line by line from stdin. I have the commands in a file named 'commands' and I want to run foo with redirecting commands to its stdin. So far I know I can do this just by simply pipe the output of the file to foo like cat commands | foo.

However I would like to give the commands one by one with some delay e.g. 10 seconds. So foo may wait for the input on the stdin and I do not care. How can I do that in bash? So basically, I want to give the inputs like interactively but not actually interactive, it is automatically.


Solution

  • With bash and a loop:

    while read -r line; do echo "$line"; sleep 10; done < <(commands | foo)
    

    or

    commands | foo | while read -r line; do echo "$line"; sleep 10; done