Use of $RANDOM in scripts with functions
Damien Dusha
d.dusha at gmail.com
Mon Oct 12 08:14:29 UTC 2009
Dear all,
I have come across some peculiar behaviour using $RANDOM in some shell
scripts (in this case, to generate different random serial numbers for
testing).
It is best explained by example. I have attached 3 shell scripts that
illustrate the behaviour:
* busybox_test_random_v1.sh uses $RANDOM in a function, where the
function is called several times
* busybox_test_random_v2.sh uses $RANDOM in several different
functions, where the functions are called once each (but do the same
thing)
* busybox_test_random_v3.sh uses $RANDOM only in the body of the script.
In _v1.sh and _v2.sh, the **same string** of numbers is generated when
calls to the function are repeated, whereas v3 behaves as expected -
each random string of digits are different. It's as though the random
number is re-seeded with the same seed each time one of those
functions are called.
For example, when running v1, I get:
unitnumber 967232
articlenumber 967232 <- Same as above
coreboard 9672324689 <- 10 digits instead of 6, but still the same
hardwarerelease 967232 <- Same 6 digits as above
when running _v2:
unitnumber 818769
articlenumber 818769
coreboard 8187697396
hardwarerelease 818769 <- Same sequence of digits
And _v3:
unitnumber 490963
articlenumber 357642
coreboard 3285917950
hardwarerelease 577456 <- All different, as desired.
Running the same scripts on bash (dash not tried) in Ubuntu 9.10 does
not exhibit the same behaviour. That is:
>From v1:
unitnumber 260060
articlenumber 967509
coreboard 8451689963
hardwarerelease 542427 <- All different, as desired
>From v2:
unitnumber 481551
articlenumber 169890
coreboard 0564598440
hardwarerelease 733998 <- All different, as desired
>From v3:
unitnumber 470484
articlenumber 256473
coreboard 5805465281
hardwarerelease 032857 <- All different, as desired
I have attached the scripts used to test these functions.
We have built busybox for an MPC5151 (PowerPC embedded device) using
DENX ppc_6xx- for the following version:
Version:
BusyBox v1.14.1 (2009-08-10 12:28:49 EST) multi-call binary
Copyright (C) 1998-2008 Erik Andersen, Rob Landley, Denys Vlasenko
and others. Licensed under GPLv2.
See source distribution for full notice.
Best regards
Damien Dusha.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: busybox_run_test.sh
Type: application/x-sh
Size: 181 bytes
Desc: not available
URL: <http://lists.busybox.net/pipermail/busybox/attachments/20091012/ef766acc/attachment.sh>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: busybox_test_random_v1.sh
Type: application/x-sh
Size: 1029 bytes
Desc: not available
URL: <http://lists.busybox.net/pipermail/busybox/attachments/20091012/ef766acc/attachment-0001.sh>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: busybox_test_random_v2.sh
Type: application/x-sh
Size: 1741 bytes
Desc: not available
URL: <http://lists.busybox.net/pipermail/busybox/attachments/20091012/ef766acc/attachment-0002.sh>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: busybox_test_random_v3.sh
Type: application/x-sh
Size: 1592 bytes
Desc: not available
URL: <http://lists.busybox.net/pipermail/busybox/attachments/20091012/ef766acc/attachment-0003.sh>
More information about the busybox
mailing list