linux script help with dates

I have a script that I have to input a date into manually in the format of '20120915'.  I only need to work with the dates from '20120815' to 20121001'.  Could someone help me put some code together that could automatically insert these dates into a variable one by one into my script?  
Maybe a for loop would work in this case?

Here is the script I am working with:

#!/bin/bash
UTIL=/cygdrive/c/apps/dcm4che-2.0.25-bin/bin
INPUT=/cygdrive/c/projects/fill/comb
BASEDIR=/cygdrive/c/projects/fill
$UTIL/dcmqr RADARCH4@10.10.51.32:104 -rStudyInstanceUID -qStudyDate=20120915 | grep "(0020,000D) UI #[0-9]*" | sed s/'(0020,000D) UI #[0-9]* \['// | sed s/'\] Study Instance UID'// | sed s/'] Study Instance'// | sed s/'UI'// | sed s/'U'// > $BASEDIR/A4_SUID
$UTIL/dcmqr RADARCH2@10.10.50.51:104 -rStudyInstanceUID -qStudyDate=20120915 | grep "(0020,000D) UI #[0-9]*" | sed s/'(0020,000D) UI #[0-9]* \['// | sed s/'\] Study Instance UID'// | sed s/'] Study Instance'// | sed s/'UI'// | sed s/'U'// > $BASEDIR/A2_SUID
comm -2 -3 <(sort $BASEDIR/A4_SUID) <(sort $BASEDIR/A2_SUID) > $BASEDIR/comb
count=0
exec 3<&0 #Save stdin to file descriptor 3.
exec 0<$INPUT # Redirect standar input.
while read line
do
input1=$(echo $line | awk '{ print $1 }')
echo "Sending :" ${input1}
$UTIL/dcmqr RADARCH4@10.10.51.32:104 -q0020000D=${input1} -cmove RADARCH2
count=$( expr $count + 1 ) 
done
exec 0<$3 #restore old stdin.
echo "Counter:" $count # Show deleted items

Open in new window


The areas where the dates get inserted are
-qStudyDate=20120915

Open in new window

doc_jayAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

woolmilkporcCommented:
First, create a variable "DATES" containing all required date strings:

start="20120815"
end="20121001"
DATES=""
for i in $(seq 0 9999)
  do
     new=$(date -d "$start + $i days" "+%Y%m%d")
     if [[ $new -le $end ]]; then DATES="$DATES $new"; else break; fi
done

Open in new window

Add the above block somewhere before line 5.

Then build a "for" loop around the affected lines: from line 5 up to wherever the loop should end.
for D in $DATES
  do
     $UTIL/dcmqr RADARCH4@10.10.51.32:104 -rStudyInstanceUID -qStudyDate=$D | grep "(0020,000D) UI #[0-9]*" | sed s/'(0020,000D) UI #[0-9]* \['// | sed s/'\] Study Instance UID'// | sed s/'] Study Instance'// | sed s/'UI'// | sed s/'U'// > $BASEDIR/A4_SUID
     $UTIL/dcmqr RADARCH2@10.10.50.51:104 -rStudyInstanceUID -qStudyDate=$D | grep "(0020,000D) UI #[0-9]*" | sed s/'(0020,000D) UI #[0-9]* \['// | sed s/'\] Study Instance UID'// | sed s/'] Study Instance'// | sed s/'UI'// | sed s/'U'// > $BASEDIR/A2_SUID
  ...
  ...
done

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
doc_jayAuthor Commented:
-woolmilkporc

  -- awesome - thanks for great help on this.  If I insert the above code into my script as below, would it just run until it hit the 'end date'?

#!/bin/bash
UTIL=/cygdrive/c/apps/dcm4che-2.0.25-bin/bin
INPUT=/cygdrive/c/projects/fill/comb
BASEDIR=/cygdrive/c/projects/fill
start="20120815"
end="20121001"
DATES=""
for i in $(seq 0 9999)
 do
  new=$(date -d "$start + $i days" "+%Y%m%d")
  if [[ $new -le $end ]]; then DATES=$DATES $new"; else break; fi
done
for D in $DATES
 do
  $UTIL/dcmqr RADARCH4@10.10.51.32:104 -rStudyInstanceUID -qStudyDate=$D | grep "(0020,000D) UI #[0-9]*" | sed s/'(0020,000D) UI #[0-9]* \['// | sed s/'\] Study Instance UID'// | sed s/'] Study Instance'// | sed s/'UI'// | sed s/'U'// > $BASEDIR/A4_SUID
  $UTIL/dcmqr RADARCH2@10.10.50.51:104 -rStudyInstanceUID -qStudyDate=$D | grep "(0020,000D) UI #[0-9]*" | sed s/'(0020,000D) UI #[0-9]* \['// | sed s/'\] Study Instance UID'// | sed s/'] Study Instance'// | sed s/'UI'// | sed s/'U'// > $BASEDIR/A2_SUID
 done
comm -2 -3 <(sort $BASEDIR/A4_SUID) <(sort $BASEDIR/A2_SUID) > $BASEDIR/comb
count=0
exec 3<&0 #Save stdin to file descriptor 3.
exec 0<$INPUT # Redirect standar input.
while read line
 do
  input1=$(echo $line | awk '{ print $1 }')
  echo "Sending :" ${input1}
  $UTIL/dcmqr RADARCH4@10.10.51.32:104 -q0020000D=${input1} -cmove RADARCH2
  count=$( expr $count + 1 ) 
 done
exec 0<$3 #restore old stdin.
echo "Counter:" $count # Show deleted items

Open in new window

0
woolmilkporcCommented:
The script would run the two lines 15 and 16 for each of the dates between the boundaries you gave, to then continue with line 18.

Is this what you desire?

I just saw that these lines create output files $BASEDIR/A4_SUID and $BASEDIR/A2_SUID.

Since the statements 15 and 6 now run more than once you must replace "> $BASEDIR/..." with ">> $BASEDIR/..." to append to the output files instead of overwriting them at each iteration!

This way you will get all the results in two single files, so I doubt that the following comm/sort stuff will still make sense. Or will it?

Wouldn't it be better to combine the results of just one pair of statements (15 and 16) and process the combined file before returning to the next pair 15 and 16?
To achieve this move "done" from your line 17 towards the end of the script, probably past line 29 (or even 30) - and don't use ">>" as suggested above! Keep using the single ">"!
0
doc_jayAuthor Commented:
--woolmilkporc

  Yes, that is the way I was wanting the script to behave, that is for the script to process only one day at a time.  I just wasn't sure where to place the "done", but I have tested it out and it works as I had hoped.

thanks for all of the help- you're very knowledgeable.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux

From novice to tech pro — start learning today.