LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 05-07-2003, 08:47 AM   #1
gumby
LQ Newbie
 
Registered: May 2003
Posts: 5

Rep: Reputation: 0
Question 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.
 
Old 05-07-2003, 12:49 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 26,953
Blog Entries: 54

Rep: Reputation: 2733Reputation: 2733Reputation: 2733Reputation: 2733Reputation: 2733Reputation: 2733Reputation: 2733Reputation: 2733Reputation: 2733Reputation: 2733Reputation: 2733
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...
 
Old 05-07-2003, 12:56 PM   #3
crabboy
Moderator
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 120Reputation: 120
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.
 
Old 05-07-2003, 03:42 PM   #4
stodge
Member
 
Registered: Jan 2002
Posts: 52

Rep: Reputation: 15
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.
 
Old 05-07-2003, 05:56 PM   #5
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
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?)
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Script - String Replacement revof11 Programming 7 11-29-2005 06:38 AM
String in Bash Shell nguyenzoro Linux - Newbie 4 11-19-2005 04:49 PM
Shell script to find a particular string Prasun1 Linux - General 5 08-30-2005 09:23 AM
string shell command? minm Linux - Newbie 10 08-19-2004 10:09 AM
Removing Text from a Shell String OrangeLefty Programming 2 04-18-2004 11:08 PM


All times are GMT -5. The time now is 12:17 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration