Solved

Need to create random six digit number for BASH script

Posted on 2011-09-29
4
520 Views
Last Modified: 2012-05-12
I have the following script:

#!/bin/bash
for i in /DVMAXMWL/*.xml
do
perl -i.bak -pe 's/ay/CR/g;s/SPS-1234/SPS-$RANDOM/g;s/PROC-1234/PROC-$RANDOM/g;s/Pre-Medication//g' $i
java -jar /securerad/dcm4chee-2.17.0-mysql/bin/editmwl.jar -a -f $i
wait
mv $i $i.parsed
done

Open in new window


I want $RANDOM to be a random six digit number.  I wan't it to use the same random six digit number where both $RANDOM variables are, currently.  I want the $RANDOM number to change for each file it processes in the /DVMAXMWL folder.  I also want the script to pause and then loop for 60 seconds.  This is beyond me but need to get it done.  Help!

Thanks
0
Comment
Question by:hypervisor
  • 3
4 Comments
 
LVL 12

Expert Comment

by:larsrohr
ID: 36883487
In bash, the built-in shell variable $RANDOM is by default between 0 and 32767 (at least in the man pages for bash that I have -- "man bash").
You can use this to create a 6-digit random number, though.

By adding 100000, you'd get a random number between 100000 and 132767.  Is that sufficient for your needs?
If you want nearly the full range of 6-digit numbers, you could first multiply by 27, and then add 100000 -- that gets you between 100000 and 984709.

So your script could be:
#!/bin/bash
for i in /DVMAXMWL/*.xml
do
myrandom=$((RANDOM * 27 + 100000))
perl -i.bak -pe "s/ay/CR/g;s/SPS-1234/SPS-$myrandom/g;s/PROC-1234/PROC-$myrandom/g;s/Pre-Medication//g" $i
java -jar /securerad/dcm4chee-2.17.0-mysql/bin/editmwl.jar -a -f $i
wait
mv $i $i.parsed
done

Open in new window


Note also that I changed to double-quotes for your perl statement.  Normally, this is to be avoided, but since you need to expand your shell variable $myrandom, and you don't have any other quoting or perl variables to worry about, it should be okay.
0
 
LVL 12

Expert Comment

by:larsrohr
ID: 36889936
Silly me, multiplying by 27 doesn't help anything.  It scatters the range over every 27th integer, that's all.  So you might as well just use:
  myrandom=$((RANDOM + 100000))


Now, as for your "wait".  You want to pause for 60 seconds, and then loop to the next xml file -- is that right?
If so, instead of "wait", use:
  sleep 60
0
 

Author Comment

by:hypervisor
ID: 36891393
I tried your sleep suggestion ... I want the app to process the files that are in the directory and then go to sleep for 60 seconds and try again ... it should never end.  I think in this case I would replace "done" with sleep 60, correct?

#!/bin/bash
for i in /DVMAXMWL/*.xml
do
myrandom=$((RANDOM * 27 + 100000))
perl -i.bak -pe "s/ay/CR/g;s/SPS-1234/SPS-$myrandom/g;s/PROC-1234/PROC-$myrandom/g;s/Pre-Medication//g" $i
java -jar /securerad/dcm4chee-2.17.0-mysql/bin/editmwl.jar -a -f $i
wait
mv $i $i.parsed
sleep 60

Open in new window

0
 
LVL 12

Accepted Solution

by:
larsrohr earned 500 total points
ID: 36892681
Oh, I see.

No, you need the "done" to complete the "for" loop.  But you can enclose the entire "for" loop within a never-ending loop, such as:

while true
do
  <your for loop>
sleep 60
done

e.g.:
 
#!/bin/bash
while true
do
  for i in /DVMAXMWL/*.xml
  do
  myrandom=$((RANDOM + 100000))
  perl -i.bak -pe "s/ay/CR/g;s/SPS-1234/SPS-$myrandom/g;s/PROC-1234/PROC-$myrandom/g;s/Pre-Medication//g" $i
  java -jar /securerad/dcm4chee-2.17.0-mysql/bin/editmwl.jar -a -f $i
  mv $i $i.parsed
  done  #end for loop
sleep 60
done  #end while loop

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now