Link to home
Start Free TrialLog in
Avatar of hypervisor
hypervisor

asked on

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
Avatar of larsrohr
larsrohr
Flag of United States of America image

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.
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
Avatar of hypervisor
hypervisor

ASKER

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

ASKER CERTIFIED SOLUTION
Avatar of larsrohr
larsrohr
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial