0

I want to create something file-like (that can be read like a common file), which everytime it is read, it gets its output from a command.

For instance, if I do echo $RANDOM > test, then I can call cat test and it will display the generated random number.

Except that I would need that everytime I call cat test (or read it any other way), my generator command is called (echo $RANDOM in my example, but actually it will generate more data -- around 1kb).

I think the best bet would be to create some kind of device special file / device driver? What I need is indeed similar to /dev/zero or /dev/random. Is it possible to create it simply? Using standard gnu/sh library would be ideal?

Thanks in advance for any hint!

1
  • I am confused at paragraph 2 it says if I do echo $RANDOM > test. However every other part of the question suggests that you don't want to do this. But may have the effect that this is done just before each read. Is that correct. Please clarify by editing the question. Apr 6, 2020 at 14:00

1 Answer 1

2

Use named pipes functionality:

mkfifo myfancypipe;
while true; do bash -c 'echo $RANDOM' > myfancypipe; done

Now go to another terminal and each time you will call:

cat myfancypipe

You will get info from echo $RANDOM

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .