LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-25-2010, 09:49 AM   #1
porkcharsui
LQ Newbie
 
Registered: Jan 2010
Posts: 9

Rep: Reputation: 0
Random date generator


Hello everyone,

I have a slight problem when trying to make a script which will generate random dummy data for testing. I'm still a n00b when it comes to linux system administration and shell scripting, but I figured out a command to generate a random date, that does exactly what I want it to do on the command prompt:
Code:
date -d "$((RANDOM%1+2010))-$((RANDOM%12+1))-$((RANDOM%28+1)) $((RANDOM%23+1)):$((RANDOM%59+1)):$((RANDOM%59+1))" '+%d-%m-%Y %H:%M:%S'
Quote:
26-03-2010 13:54:47
But as soon as I put it in a Bash shell script and run that script(even if I comment out everything else), it generates only one date:
Quote:
01-01-2010 01:01:01
I suspect it has something to do with the formatting of the command for scripting and I've been trying alot of variations for placing, (), "", '', ``, etc., but I can't seem to figure out why it doesn't work.

Can someone please explain to me what I'm doing wrong!!! I'm greatful for any help anyone might have.

Thanx, PorkCharSui
 
Old 01-25-2010, 10:15 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
It works just fine for me, copied&pasted into a bash script. The problem doesn't seem to be with your syntax.

I'm guessing it has something to do with your $RANDOM environment variable. Perhaps it's not getting exported properly so the script can use it or something. Try putting an "echo $RANDOM" in your script to test it.
 
Old 01-25-2010, 10:19 AM   #3
kforbus
Member
 
Registered: Sep 2009
Location: Maryland
Distribution: Slackware
Posts: 68

Rep: Reputation: 29
Since your command is only printing one date, you could create a bash script to generate more dates.

Here's an example with your command:

Code:
#!/bin/sh
i=1
while [ $i -lt 10 ]
do
date -d "$((RANDOM%1+2010))-$((RANDOM%12+1))-$((RANDOM%28+1)) $((RANDOM%23+1)):$(
(RANDOM%59+1)):$((RANDOM%59+1))" '+%d-%m-%Y %H:%M:%S'
i=$(( $i + 1 ))
done
The above starts out with i equal to 1. As long as i is less than 10 it will run your command and increment i by 1. If you want more or less dates to be generated, you can just change the number 10 in the above code to be whatever you want. Just save the code above into a new file and then run
Code:
chmod +x your_filename
, and then
Code:
./your_filename
to run it.

Hope this helps.
 
Old 01-25-2010, 10:42 AM   #4
porkcharsui
LQ Newbie
 
Registered: Jan 2010
Posts: 9

Original Poster
Rep: Reputation: 0
Thanx for your help David the H. and kforbus... it seems that I've been stupid! Instead of running the script with "./"<filename> like normal ppl do, I ran my script using "sh" <filename>. Apparently this makes a big difference on the script. But that's what I'm n00b for right.
 
Old 01-26-2010, 12:33 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
When you launch a script with "sh", it usually means to treat it as a posix script, that is, restricted to features defined in the posix standards. Apparently (from a quick google search) $RANDOM is a non-posix (but widely-available) shell extension. If you want to generate a random number in a posix-compliant script, you'll need to use an external tool such as awk.

The first line in most scripts, the "sha-bang" as it's often called, tells your system which interpreter to use. "so #!/bin/bash" means run the script in bash, using all it's available features, and "#!/bin/sh" means to treat it as a posix script (bash is still the interpreter in most distros, but sh restricts it to posix mode). Similarly, you'll find "#!/bin/awk" "#!/bin/perl", etc., for scripts of those types.

As you seem to have discovered, you usually only need to specify the interpreter in the command line if the script doesn't have a sha-bang defined.

Generally you'll want to use /bin/bash instead of /bin/sh for your user-mode scripts, unless you need to make it portable to systems that don't have bash; other unixes and such.

Last edited by David the H.; 01-26-2010 at 12:34 AM.
 
Old 01-26-2010, 02:58 AM   #6
porkcharsui
LQ Newbie
 
Registered: Jan 2010
Posts: 9

Original Poster
Rep: Reputation: 0
Thanx for the explenation David! I saw there was a difference but didn't know what it was, now I understand what was going wrong.
 
  


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
a good random number generator in C++ asmahan Linux - Software 2 08-24-2009 02:40 PM
I need a random number generator newbiesforever Linux - Software 5 05-22-2009 03:04 PM
random number generator vkmgeek Programming 3 09-02-2008 10:41 AM
Random Sentence Generator? gandil Linux - Newbie 7 01-01-2006 11:14 AM
Help: Need random password generator linuxgamer Linux - Software 5 12-02-2003 02:01 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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