Execute Program until specific amount of bytes has been returned on stdout, then terminate
0
votes
1
answer
101
views
Imagine I have the following program/script
./generate-infinite-byte-stream
:
#!/bin/bash
echo -n 'hello'
sleep infinity
The infinite sleep command represents a network connection that may or may not deliver more data in the indefinite future that I am not interested in.
I would like to have a program, let's call it take 5
that runs ./generate-infinite-byte-stream
until it has output 5 bytes on stdout and then terminates it:
take 5 ./generate-infinite-byte-stream
# gives 'hello' and returns with exit code 0
Is there such a program or do I need to roll my own with popen()? The program take
should also redirect stdin to the executed program.
Note: head -c 5
does not do the right thing, because it does not terminate:
./generate-infinite-byte-stream | head -c 5
# this returns 'hello', but never terminates
Aside: The name Take is inspired by the https://reference.wolfram.com/language/ref/Take.html command which returns the first n elements of a list.
Asked by masterxilo
(137 rep)
Jun 4, 2021, 09:48 AM
Last activity: Jun 4, 2021, 10:13 AM
Last activity: Jun 4, 2021, 10:13 AM