Imagine I have the following program/script ./generate-infinite-byte-stream:

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.

  • That script is very poorly named. If it really did produce more output, then it would be terminated by trying to write to a closed pipe. Jun 4 at 9:59
  • @TobySpeight You mean the ./generate-infinite-byte-stream script? That's fine. You could even imagine that it said echo hello; sleep 1; echo world; sleep infinity. I do not care about the extra output after the first 5 bytes and I want to completely ignore any errors that might occur and terminate that program/script as soon as possible after the first 5 bytes have been returned, ignoring all the rest.
    – masterxilo
    Jun 4 at 10:04
  • I don't know of a program that does what you want - it's usually sufficient to close the stream, meaning that the 6th byte of output will kill the producer with SIGPIPE. To terminate the producer immediately after byte 5, rather than at byte 6, will likely require a simple program. Jun 4 at 10:10

If your shell is Bash, you could use a process substitution. Demo:

dd bs=1 count=5 < <(printf hello; sleep infinity)

That doesn't kill the producer, but does disconnect it so that the dd command completes. This may or may not be what you need.

  • Thank you, that already helps as a starting point. But I will need a solution that kills the producer, otherwise the system will get littered with open network connections waiting for irrelevant data and lots of running processes.
    – masterxilo
    Jun 4 at 10:27
  • Yes, then you'll need to write a small program to signal the writer when you've read enough. Jun 4 at 10:34

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.