concatenate files sorted by date

I am a beginner in script writing, i tried to do the following
I have a set of files sorted by date in the format YYMMDD.s and .x and .r
I need to concatenate a header file to these sets of files so I used the following code

echo "enter Swath number"
read s
echo "please enter first date and MMDD press ENTER"
read i
echo "please enter last date and MMDD  press ENTER"
read j
for((i; i <= j; i++))
do
cat NJSC_Alam_Ph1_Blk1_3D_HDR_S /ud/SPECSDATA/Final_SPS/Alam3D/Swath_${s}/08${i}.s  > /ud/SPECSDATA/Final_SPS/Alam3D/Swath_${s}/08${i}_Final.s

cat NJSC_Alam_Ph1_Blk1_3D_HDR_R /ud/SPECSDATA/Final_SPS/Alam3D/Swath_${s}/08${i}.r  > /ud/SPECSDATA/Final_SPS/Alam3D/Swath_${s}/08${i}_Final.r

cat NJSC_Alam_Ph1_Blk1_3D_HDR_X /ud/SPECSDATA/Final_SPS/Alam3D/Swath_${s}/08${i}.x  > /ud/SPECSDATA/Final_SPS/Alam3D/Swath_${s}/08${i}_Final.x

done

I expect it to concatenate the files based on the user input.
I have 2 problems, the first on is that it doesn't do this for example if I enter the value for i and j to be 0530 and 0531 for 30 and 31 of May it searches for a file named 08345 instead of 080530.
the other problem is how can i make the script to work for 2 different months? like I have the first file as 300508 and last file as 050608, the loop is not going o work there

Problem number 1 is you're mixing date math with integer math. You cannot add 1 to your "0530" and expect to get the next day. For example, there is no integer "0530". There is an integer "530", however, and if you add 1 to it you will get "531". ...NOT the "0531" that you need. And, as you mentioned, adding 1 to that will not get you into June.

The problem of shell date arithmetic is fairly difficult. See "Date math in Linux shell script?": Tech Support from Ask Dave Taylor! .

When you do your comparison, you can test for your end condition in either of two ways:

  • If the current date == the end date (string comparison)
  • If the current iterations number of seconds since the epoch is less than or equal to the end date's number of seconds since the epoch.

Also, here's a couple of more gotchas you need to be aware of. If you do your date math using "number of seconds since the Epoch",

  • You may want your first date to actually be midnight of the first date- that is, the 0'th second of that day.
  • You may want your second date to actually be 23:59:59 on that day.

This is because if you do your "begin <= end" comparison, you may be comparing 3pm on the end day to 2pm on the end day and that iteration of the script will not run.

I mention that because it's convenient to actually do the date math by:

  1. Convert the first date to the number of seconds since the epoch
  2. Increment the date by adding 86,400 (the number of seconds in a day) to the date for each iteration.

You also must be aware of when the year changes. Don't forget you may be comparing dates in January to dates in December.
-mschwage

thank you for the reply, I get what you want to say fine, just one request:
what is the script command to identify a date variable? is there a variable called date?
I tried something like date i, and dim i as date but it didn't work.

I also tried "date -d ${i} +"%y%m%d"" but it said that i is not a command

can you please help me on that?