What I'd like to do is send a single file "repeatedly" (like cat'ing it an infinite number of times) as input to another program. Is there a way on the command line/using bash?
问题:
回答1:
Process substitution provides a mechanism by which bash can generate a temporary, readable filename connected to an arbitrary chunk of bash code for you:
./my_program -input <(while cat file_to_repeat; do :; done)
This will create a /dev/fd/NN
-style name on operating systems that support it, or a named pipe otherwise.
回答2:
The yes
command, using the file's contents as it's argument:
yes "$(<file)" | somecommand
回答3:
Yes.
while [ true ]; do cat somefile; done | somecommand
回答4:
Appears it is possible through the use of mkfifo
(this way allows for easy control, restartability, and large files)
$ mkfifo eternally_looping_filename # you can name this what you want.
Then write to that fifo "looping" from one bash prompt, ex: create script named bash_write_eternal.sh:
while [ true ]; do
cat /path/to/file_want_repeated > ./eternally_looping_filename
done
run that in one terminal
$ ./bash_write_eternal.sh
(you could background it also if you want to reuse the same terminal)
then in another terminal, run your input program, like
$ ./my_program -input ./eternally_looping_filename
or
$ cat ./eternally_looping_filename | ./my_program
your program will now receive an eternal input of that file looping over and over. You can even "pause" the receiving program by interrupting the terminal running the bash_write_eternal.sh
script (its input will be suspended until you resume the fifo writing script).
Another benefit is "resumable" between invocations, and also if your program happens to not know how to receive input from "stdin" it can receive it from a filename here.
回答5:
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