LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
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
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
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: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:15 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration