I’m trying to read some bytes out of /dev/urandom, keep only the ones I can type easily, and trim the result to 30 characters. I can’t figure out how to get the “only 30 characters” behavior when the data is coming from a pipe, though. I tried
cat /dev/urandom | tr -cd 'A-Za-z0-9' | cut -c -30
cut -c -30 /dev/urandom | tr -cd 'A-Za-z0-9' | cut -c -30
but these hang without displaying anything. On the other hand,
cut -c -30 /dev/urandom | tr -cd 'A-Za-z0-9'
outputs data endlessly (although it’s clearly been processed by
tr because only the specified characters appear in the output).
In general, how can I read a certain number of characters (or bytes) from a pipe and then close the pipe?