LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   creating a unique id string in shell (https://www.linuxquestions.org/questions/programming-9/creating-a-unique-id-string-in-shell-58565/)

gumby 05-07-2003 08:47 AM

creating a unique id string in shell
 
How would I generate a unique id string within a Bash shell? PHP has a nice function called uniquid() that generates a 13 digit unique string. Any suggestions for how to do this in Linux shell scripting? I found random(), but was unsure how to use it. Thanks for any assistance.

unSpawn 05-07-2003 12:49 PM

r=( $(openssl rand 100000 | sha1sum) ); printf "%s${r[0]:0:13}\n"

If you haven't got|don't want OpenSSL, and you can't|won't use designated devices like /dev/random or the Entropy Gathering Daemon then try using data that changes a lot, like catting disk/network stats from /proc, time, etc etc...

crabboy 05-07-2003 12:56 PM

You can use the $RANDOM shell variable, but it is not guaranteed to be unique. Not sure what you need it for, but if is for temporary file names you could try mktemp.

stodge 05-07-2003 03:42 PM

I don't know of a solution, but search Google for a GUID generator. You might find one that you can port to the language/platform of your choice and then execute it from the within the shell.

Hko 05-07-2003 05:56 PM

Re: creating a unique id string in shell
 
Quote:

Originally posted by gumby
How would I generate a unique id string within a Bash shell?
- If it doen't need to be random, but just unique, how about a simple counter? Increase by 1 each time you need another identifier, padding with zero's if you need the id's to be fixed length strings.

- If you just need one unique id for each instance of the script, how about it's process id? You get it (in bash/sh) easily with the $$-variable, e.g. "echo $$".

(or maybe I didn't get your question right?)


All times are GMT -5. The time now is 03:33 AM.