Solved

Need to create random six digit number for BASH script

Posted on 2011-09-29
4
506 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

706 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

17 Experts available now in Live!

Get 1:1 Help Now