create infinite looping repeating file cat in linu

2019-05-01 19:10发布

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?

标签: linux bash cat
5条回答
唯我独甜
2楼-- · 2019-05-01 19:48

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.

查看更多
相关推荐>>
3楼-- · 2019-05-01 19:50

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
查看更多
小情绪 Triste *
4楼-- · 2019-05-01 19:52

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.

查看更多
该账号已被封号
5楼-- · 2019-05-01 19:54

Yes.

while [ true ]; do cat somefile; done | somecommand
查看更多
冷血范
6楼-- · 2019-05-01 20:04

The yes command, using the file's contents as it's argument:

yes "$(<file)" | somecommand
查看更多
登录 后发表回答