Imagine I have the following program/script
#!/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.
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.