[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 626
  • Last Modified:

Need to create random six digit number for BASH script

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
hypervisor
Asked:
hypervisor
  • 3
1 Solution
 
larsrohrCommented:
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
 
larsrohrCommented:
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
 
hypervisorAuthor Commented:
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
 
larsrohrCommented:
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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now