Create an endless loop repeating a cat file in linux / bash

What I would like to do is send one file “repeatedly” (for example, infinitely many codes) as input to another program. Is there a way on the command line / using bash?

+5
source share
5 answers

Replacing a process provides a mechanism by which bash can generate a temporary, readable file name connected to an arbitrary piece of bash code for you:

./my_program -input <(while cat file_to_repeat; do :; done) 

This will create the style name /dev/fd/NN on operating systems that support it, or a named pipe otherwise.

+3
source

yes command , using the contents of the file as an argument:

 yes "$(<file)" | somecommand 
+5
source

Yes.

 while [ true ]; do cat somefile; done | somecommand 
+3
source

Appears possibly with mkfifo (this method makes it easy to manage, restart and large files)

 $ mkfifo eternally_looping_filename # you can name this what you want. 

Then write that fifo is " looping " from one bash hint, for example: create a script called bash_write_eternal.sh:

 while [ true ]; do cat /path/to/file_want_repeated > ./eternally_looping_filename done 

run it in one terminal

 $ ./bash_write_eternal.sh 

(you can use it also if you want to reuse the same terminal)

then in another terminal run the input program, for example

 $ ./my_program -input ./eternally_looping_filename 

or

 $ cat ./eternally_looping_filename | ./my_program 

your program will now receive the eternal input of this file loop over and over. You can even “pause” the receiving program by interrupting the terminal on which the bash_write_eternal.sh script is running (its input will be suspended until you resume writing the fifo script).

Another advantage is the "resume" between calls, and also, if your program does not know how to get input from "stdin", it can get it from the file name here.

+1
source

A while : loop repeats forever:

 while : do cat input.txt done | your-program 

To use a helper function:

 repeat() while :; do cat "$1"; done repeat input.txt | your-program 
0
source

Source: https://habr.com/ru/post/1239620/


All Articles