Quote:
Originally Posted by IdOp
Interesting thread!
At first I thought that having the dd command at the end with bs=16 would make it stop reading from /dev/urandom when it had found 16 (allowed) characters. That would be a nice feature, but then thinking about it more I became less sure that would happen. I thought the first command in the pipeline, strings, might very well read large blocks from /dev/urandom and start feeding it down the pipe, only to have the thing terminate after a small number of bytes had been processed. But how to check this?
After a couple of failed attempts, I think I found something that works. Change the pipeline to this:
Code:
< /dev/urandom tee file.tmp | strings -n1 | tr -dc _A-Z-a-z-0-9 | dd count=1 bs=16 2>/dev/null;echo
(Make sure file.tmp does not already exist!) The tee command is like a cat that also copies everything going through it to the given file. So by checking the size of file.tmp you can see how many bytes were read from /dev/urandom (or at least get a lower bound). (I'm assuming tee reads similarly to how strings would.) Here are the file sizes I observed:
Linux: 49152
OpenBSD: 196608
|
So I tweaked your tee stuff in a way I think allows you to measure how much strings pulls at a time:
Code:
< /dev/urandom strings -n1 | tee file.tmp | tr -dc _A-Z-a-z-0-9 | dd count=1 bs=16 2>/dev/null;echo
which gave a file.tmp size on OpenBSD of 80K; using tee before strings got one on my box of 184K. Similarly on Linux it gave me ~30K while starting with tee gave me ~65K. So strings seems to take smaller chunks than tee I guess?
Anyways, I'm now just taking 500 out of /dev/random which gives me a final file that's ~100 characters on linux, plenty of wiggle room and 500 isn't that bad a pull:
Code:
dd if=/dev/urandom count=1 bs=500 2>/dev/null | strings -n1 | tee file.tmp | tr -dc _A-Z-a-z-0-9 | tee file.tmpp | dd count=1 bs=16 2>/dev/null;echo
I'm excited to use the new silent dd option ("status=noxfer") that just went into openbsd to make it compatible with gnu to stop redirecting std err to null; unfortunately not compatible with freebsd/netbsd though