Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

"uniqueness" question 2

Status
Not open for further replies.

ns705

Programmer
Feb 4, 2004
29
GB
Hi,

can you please help me with one question?

I need to create the files with completely unique names.
I attempted to use a PID of some command as this unique number, but it does not help - I faced with problem when the system uses the selected PID in some minutes for some other process...

Is it possible to obtain some real unique number or char combination in ksh shell?

Thank you in advance.

Anta
 
Something like this ?
num=$(awk 'BEGIN{srand();print int(10000*rand()+'$$')}')
The same PID ($$) and the same time (srand) is unlikely.

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Thank you, PHV.

Yes, something about it.
In my case the current shell is the same so $$ contains the same number.
I hope this will help me.

Thank you a lot.

Anta
 
The ksh has a built-in function called RANDOM

e.g echo $RANDOM

you'd have to be very unlucky to get a duplicate with this

filename=`date "+%Y%m%d-%H%m%S-$$-$RANDOM`

echo $filename

--
| Mike Nixon
| Unix Admin
|
----------------------------
 
Thank you, Mike.

Do you think it would be more foolproof method that use srand and rand awk functions?
The problem is a lot of unique values should be created during the same second in the same shell.
And you will laugh but it is real matter - I have received a duplicate wia awk.

Maybe I am a unlucky champion today.

Anta
 
Perhaps use a sequence number that is read from a file, incremented, then written back to the file. e.g...

echo $(($(<seqfile)+1))|tee seqfile|read seq

Some file-locking may be required if the script is being run concurrently.
 
Thank you, Ygor.

I thought about something in this line.
But in this case I will should have some locking on this seqfile too. A lot of concurrent sub-process will try to read/write from it.

Anta
 
I'm of the kis school of though "Keep It Simple"

you could go on for ever but

echo $RANDOM$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM

seems a bit overkill to me ;-)

--
| Mike Nixon
| Unix Admin
|
----------------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top