Solved

Need to create random six digit number for BASH script

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This Windows batch file is useful for organizing image files from a digital camera or other source, but can have many other uses.  It simply renames the file(s) to match their create date.  For example, if you took a picture today at 1:40pm and the …
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…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

860 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