1

I was trying to create a file with a streaming interface just like /dev/random. Basically, I provide some initial contents to the file, and when a program reads from the file, it consumes some bytes, so others can only read from the rest of the file. Is there a way to do this?

  • You mean a pipe? – phk Nov 16 '16 at 21:16
1

Yes, you are describing a named pipe, also known as a FIFO.

bash-4p4$ mkfifo mypipe

bash-4.4$ ( dd if=/dev/random of=mypipe ) &
[1] 37688

bash-4.4$ hexdump -C mypipe | head -n 2
00000000  e0 de 4a 6e 48 63 aa 25  21 77 89 85 53 fd 36 57  |..JnHc.%!w..S.6W|
00000010  c3 be 55 f3 15 00 6b 44  64 27 c4 d8 8c 7d de d5  |..U...kDd'...}..|

bash-4.4$ hexdump -C mypipe | head -n 2
00000000  dd ff 66 1d 15 f2 0b 55  ee 94 f2 f1 96 60 e9 8a  |..f....U.....`..|
00000010  a0 b9 9d 1d 08 22 0b 33  ad dc 94 c1 61 49 e0 1b  |.....".3....aI..|

bash-4.4$ hexdump -C mypipe | head -n 2
00000000  82 7e 99 20 30 65 b9 eb  35 bd 6f 6b b6 bf 7e 2a  |.~. 0e..5.ok..~*|
00000010  5e c2 97 b4 e6 47 be 85  43 ab d7 00 be 86 f2 ae  |^....G..C.......|

bash-4.4$ hexdump -C mypipe | head -n 2
00000000  3c 22 69 e2 95 64 6f 4e  e4 3d 69 8c eb 68 c7 2f  |<"i..doN.=i..h./|
00000010  b5 76 4d 81 59 db 06 9b  e7 97 85 01 db c3 e0 7b  |.vM.Y..........{|

bash-4.4$ kill %1
bash-4.4$
[1]+  Terminated              ( dd if=/dev/random of=mypipe )

bash-4.4$ rm mypipe

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.