creating a random variable in shell script

So I've been working alot with shell scripting, and needed to define a random variable in a script so I could prepend some file names with a unique identifier to prevent other executions of the script from overwritting files.

Well shell scripting is really just glorified qbasic in the programming world, but I think I ran across an interesting problem and wanted to share my solution.

ps=`ps -ef | sum | cut -c4-5`
date=`date +%H%M%S`
ps2=`echo "$ps" + 1 | bc`
rndm=`echo "$date" / "$ps2" | bc`

So there you go....the variable $rndm is now random. Originally I was going to use `expr` but after running several test I ran into quite a few bugs with floating decimal points and the fact that `ps -ef` can in fact return a zero which leads to dividing by zero.....and we all know how that turns out...

Leave a comment

Recent Entries

Close