So, I was at a CLUG meeting last night, and one of the speakers had a whole bunch of bash scripts for XDM theming. Anyway, he was using a perl script to generate the random selection of the theme elements, and me and my big mouth offered that it could be done in bash itself. So here we are…
Here’s my post to the CLUG mailing list the next day:
From mikal@stillhq.com Fri Mar 29 10:26:04 2002
Date: Fri, 29 Mar 2002 10:14:17 +1100 (EST)
From: Michael Still
To: Linux user groupSubject: Nemo’s bash challenge for the day Well, I said it could be done…
The brief: Generate a random number, and then return that element from a
list of elements, in bashThe code: (Assuming that the arguements on the command line are the
possible return options, and that the random number generator is running
as a separate script)LOBOUND=0
HIBOUND=$#
shift $(( $LOBOUND + ($HIBOUND * $RANDOM) / (32767 + 1) ))
echo $1See the attachments for some exploratory scripts I wrote while coming up
with this truncated sh. There are 54 lines of comments / white spaces, to
the 4 or so lines of actual code.Have a nice life…
Cheers,
MikalPS: How good is the bash random? See the attachment output.count for a
summary of 100,000 numbers between 1 and 10 being generated with the
default seed. It’s probably good enough for most people.PPS: If you want the code, it should also be online at
http://www.stillhq.com/cgi-bin/getpage?area=bashrand&page=index.htm in
about 30 minutes, depending when Andrew goes to sleep.—
Michael Still (mikal@stillhq.com) UMT+11hrs
[ Part 2, “” Application/X-SH 2.1KB. ]
[ Unable to print this part. ][ Part 3, “” Application/X-SH 412bytes. ]
[ Unable to print this part. ][ Part 4, “” Text/PLAIN (Name: “output.count”) 2 lines. ]
[ Unable to print this part. ]
I have included some explaination of the development process I went through below for those who are interested…