LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 12-21-2014, 02:17 PM   #1
Tasker
LQ Newbie
 
Registered: Dec 2014
Location: usa
Distribution: ZorinOS, Tails
Posts: 9

Rep: Reputation: 1
why $$ doesn't generate a new random number


I don't need help to generating a random number to add to a file name because there are many ways and I know a few methods of doing so. However, I am particularly interested in the double dollar sign '$$'.

for example, I wanted to make a few short clips from a video and I wanted the filename to have a random number for each filename.

Quote:
ffmpeg -i video.mp4 -t 300 -vcodec copy -acodec copy out-$$.mp4
it created a file called out-28079.mp4

When I ran the code again to generate another file, ffmpeg complain

Quote:
File 'out-28079.mp4' already exists. Overwrite ? [y/N]
I though the double $$ sign always generate a new random number on each execution. Am I using the $$ characters wrong?

For now, I have to use the other methods for a random number.

Last edited by Tasker; 12-21-2014 at 02:23 PM.
 
Old 12-21-2014, 02:25 PM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,264
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Why would you think it should give a random number?

$$ is the process ID of the shell.

See man bash and search for random, but as I recall $RANDOM is what you want.
 
1 members found this post helpful.
Old 12-21-2014, 02:43 PM   #3
Tasker
LQ Newbie
 
Registered: Dec 2014
Location: usa
Distribution: ZorinOS, Tails
Posts: 9

Original Poster
Rep: Reputation: 1
I though the $$ characters acted like the $RANDOM variable. I was wrong. It's been a long, long time since I last use the $$ characters.

Here is an excerpt from an article which explain why I wanted to use the $$ characters.

Quote:
Creating Good Temporary File Names:

The $TMPDIR variable contains /tmp (or ~/tmp or a similar directory, depending on availability). This should be used for the location of the temporary file.

It is often a good idea to create a temporary directory (inside of $TMPDIR) if there is a utility that does this securely; then you can add as many temporary files of any name to that directory without worry.

It is a good idea to use the name of the program and the process ID (PID) as part of the file name. Use the $$ shell variable as part of the file name. This identifies which process is responsible for the file, and is useful as a debugging aid. The PID never changes for the life of a shell script, and helps make unique names. (Imagine running a script in two windows at the same time.)

To make the name highly unpredictable, append a random number to the file name. Use the $RANDOM shell variable as part of the name. Using these techniques, file names are both easily identifiable and unpredictable.

The following line of code creates the temporary files using these guidelines:

new=$TMPDIR/swatch-new-$$.$RANDOM
old=$TMPDIR/swatch-old-$$.$RANDOM
You're right the $$ variable is for the process ID of the program.

Last edited by Tasker; 12-21-2014 at 02:51 PM.
 
Old 12-21-2014, 03:18 PM   #4
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,264
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
It is difficult to keep them all straight!

By the way, welcome to LQ!!
 
Old 12-21-2014, 03:48 PM   #5
Tasker
LQ Newbie
 
Registered: Dec 2014
Location: usa
Distribution: ZorinOS, Tails
Posts: 9

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by astrogeek View Post
It is difficult to keep them all straight!

By the way, welcome to LQ!!
Thanks and a +1
 
Old 12-21-2014, 04:53 PM   #6
Miati
Member
 
Registered: Dec 2014
Distribution: Linux Mint 17.*
Posts: 326

Rep: Reputation: 106Reputation: 106
If you don't want large random numbers (5 digits long) you can change the parameters of random:
To make it between 0 & 99 -> $((RANDOM%99))

Also, if you are using this in a script I would suggest using a while loop to ensure the filename doesn't exist causing a problem:
Code:
filen=""$RANDOM"_file.mp4"
while [ -e $filen ]
  do
    filen=""$RANDOM"_file.mp4"
done

ffmpeg -i video.mp4 -t 300 -vcodec copy -acodec copy "$filen"
 
1 members found this post helpful.
Old 12-21-2014, 06:31 PM   #7
Tasker
LQ Newbie
 
Registered: Dec 2014
Location: usa
Distribution: ZorinOS, Tails
Posts: 9

Original Poster
Rep: Reputation: 1
I like the while loop part to check on an existing file and to pick another random number until it finds a non existing number for the filename.

Thanks Miati and a +1 for the extra info.
 
  


Reply



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
[SOLVED] Generate Long Digits Random Number in C / C++ devUnix Programming 25 09-20-2011 01:20 PM
[SOLVED] I need to generate random number either 1 or 0? mvmacd Linux - General 4 01-21-2010 06:50 PM
Generate a random number from a bourne shell script lothario Linux - Software 2 03-01-2007 11:01 PM
Generate random number with C++ TruongAn Programming 5 11-09-2005 12:01 AM
Python problems trying to generate a random number davholla Programming 0 10-27-2003 04:07 AM

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

All times are GMT -5. The time now is 12:26 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