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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
01-25-2010, 09:49 AM
|
#1
|
LQ Newbie
Registered: Jan 2010
Posts: 9
Rep:
|
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'
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:
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
|
|
|
01-25-2010, 10:15 AM
|
#2
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852
|
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.
|
|
|
01-25-2010, 10:19 AM
|
#3
|
Member
Registered: Sep 2009
Location: Maryland
Distribution: Slackware
Posts: 68
Rep:
|
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 to run it.
Hope this helps.
|
|
|
01-25-2010, 10:42 AM
|
#4
|
LQ Newbie
Registered: Jan 2010
Posts: 9
Original Poster
Rep:
|
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.
|
|
|
01-26-2010, 12:33 AM
|
#5
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852
|
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.
|
|
|
01-26-2010, 02:58 AM
|
#6
|
LQ Newbie
Registered: Jan 2010
Posts: 9
Original Poster
Rep:
|
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.
|
|
|
All times are GMT -5. The time now is 07:57 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|