Bug#695285: /etc/cron.daily/apt backup RNG is very wasteful
> Thanks for checking this. I like this, its more compact than the
> current code. However the reason why I did not use this, was that the
> following fragment:
>
> $ while true; do res=$(od -N2 -d /dev/urandom | cut -s -d' ' -f2); echo
> $res; if [ -z "$res" ]; then break; fi ; done
> 61581
> 42056
> 38021
>
> $
>
> give me a empty string in $res sometimes. I haven't really put any
> though into why this is, maybe just a side effect of running it in th
> while loop?
No, it's a side effect of od printing a decimal number which is sometimes less than 10000 and so
is space-padded on the left.
Try:
while : ; do x=$(od -N2 -d /dev/urandom); y=$(echo "$x" | cut -s -d' ' -f2); echo $y; if test -z "$y"; then echo "$x"; break; fi; done
When it errors out, $x contains:
0000000 9611
0000002
Now is it clear?
Include *all* the fields other than 1 and it will never fail:
while :; do res=$(od -N2 -d /dev/urandom | cut -s -d' ' -f2-); echo $res; if [ -z "$res" ]; then break; fi ; done
The other way is to use a number format that is zero-padded:
while :; do res=0x$(od -N2 -x /dev/urandom | cut -s -d' ' -f2-); echo $res; if [ -z "$res" ]; then break; fi ; done
Reply to: