views:

86

answers:

3

Hi all,

I need to generate 12 digit Hex numbers in KSH on Solaris

Thanks

A: 

Start with this Python program, hex12.py.

hex12.py

#!/usr/bin/env python
import random
import hashlib
h= hashlib.sha1(str(random.random())).hexdigest()
print h[:12]

In your shell you can now use hex.py to create 12 hex digits on standard out.

S.Lott
@S. Lott, I believe he requires a solution using ksh script.
Anders
He asked for ksh, I think he needs a korn shell script
klez
That works in ksh. Try it. If this has `x` permissions, then the `#!/usr/bin/env python` causes ksh to invoke Python. Every time. Reliably. Indeed, it works in just about every shell.
S.Lott
@S. Lott, It's a mute point. You could write it in Java and it would work, he required a solution in KSH scripting language, otherwise I'm sure he would said something.
Anders
@Anders: It wouldn't trivially work in Java. You have to compile it and make an executable JAR from it. In Python, it works as if it's part of the ksh.
S.Lott
A: 

Try this one:

DIGITS=`head -c 6 /dev/urandom | od -x | head -n 1 | sed -e 's/^0* //' -e 's/ //g'
Joshua
+3  A: 
#!/bin/ksh
set -A hex 0 1 2 3 4 5 6 7 8 9 A B C D E F
for i in {1..12}
do
   printf ${hex[$((RANDOM%16))]}
done
ghostdog74
Good solution except I would probably add an additional line before the loop, i.e. RANDOM=0x`head -c 4 /dev/urandom | od -x | head -1 | cut -d' ' -f6`
fpmurphy
This is working good, I will give it a try once I am at work tomorrow Thanks
rojanu
That should be modulus 16. You can omit the `$(())`: `${hex[RANDOM%16]}`. You can also do `hex=({0..9} {A..F})`. (These are true in ksh93. I'm not sure about earlier versions.)
Dennis Williamson
This doesn't work on a Solaris box/bin/ksh Version M-11/16/88i/usr/dt/bin/dtksh Version M-12/28/93dTested on both SunOS 5.10 Generic_142901-11 i86pc i386 i86pcSunOS 5.9 Generic_118558-27 sun4u sparc SUNW,Sun-Fire-280R
rojanu