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.
|
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... |
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.
|
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.
|
Re: creating a unique id string in shell
Quote:
- 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. |