Howto shorten script in a busybox environment by using for loops?

My satellite receiver is equipped with busybox, so a small linux version.
That is why I can not use certain commands like #tomorrow in date commands or #date -d "+1 day"
and thus I have to use: day1=$[$day0+1]

I want to download every day 6 files from the internet but the filenames consist of the date of today and the next five days.

I have written a bash script which does what I want. However it is straight written and can be smaller I think. I do not know how to calculate with variables in a for loop. Below is a part of the script I wrote and in the script

day=`date +"%Y%m%d"`
month=`date +"%m"`
year=`date +"%Y"`

days_in_month()                # maximum days of every month
{
  case $month in
    09|04|06|11) maxdays=30 ;;
    01|03|05|07|08|10|12) maxdays=31 ;;
    02) if (( ("$year" % 400) == "0" )) || (( ("$year" % 4 == "0") && ("$year" % 100 != "0") )) ; then
          maxdays=29
        else
          maxdays=28
       fi
  esac
}

check_end_of_month_year()        # check if it will be end of month/year
{
  if [ $day_temp -gt $maxdays ]; then # reached end of month
    day_temp=01
    month=$[$month+1]
    if [ $month -gt 12 ]; then       # reached and of year
      month=1
      year=$[$year+1]
    fi
    new_month=yes            # 
    days_in_month            # calculate max days in new month
  fi
  month=`printf "%02d" $month`
}

days_in_month                   # how many days does current month have
new_month=no                  # parameter to indicate new month or not
day0=`date +"%e"`            # put date of today in day0
FILE0=$day"_US.gif"            # real filename for FILE0 download

# ######################################
# ------- I think the source below can be smaller -------
# ######################################

day1=$[$day0+1]                # day1 = day of today + 1 so tomorrows date
day2=$[$day1+1]                # day2 = day of day1 + 1
day3=$[$day2+1]                # day3 = day of day2 + 1
day4=$[$day3+1]                # day4 = day of day3 + 1
day5=$[$day4+1]                # day5 = day of day4 + 1

if [ $new_month = yes ]; then
  day_temp=$[$day_temp+1]        # day_temp will be used in check_end_of_month_year
else
  day_temp=$day1               # day_temp will be used in check_end_of_month_year
fi
check_end_of_month_year            # check if it will be end of month/year
day1=`printf "%02d" $day_temp`        # day1 = daytemp but with leading zero
FILE1=$year$month$day1"_US.gif"        # real filename for FILE1 download

if [ $new_month = yes ]; then
  day_temp=$[$day_temp+1]        # day_temp will be used in check_end_of_month_year
else
  day_temp=$day2               # day_temp will be used in check_end_of_month_year
fi
check_end_of_month_year            # check if it will be end of month/year
day2=`printf "%02d" $day_temp`        # day2 = daytemp but with leading zero
FILE2=$year$month$day2"_US.gif"        # real filename for FILE2 download

if [ $new_month = yes ]; then
  day_temp=$[$day_temp+1]        # day_temp will be used in check_end_of_month_year
else
  day_temp=$day3            # day_temp will be used in check_end_of_month_year
fi
check_end_of_month_year            # check if it will be end of month/year
day3=`printf "%02d" $day_temp`        # day3 = daytemp but with leading zero
FILE3=$year$month$day3"_US.gif"        # real filename for FILE3 download

if [ $new_month = yes ]; then
  day_temp=$[$day_temp+1]        # day_temp will be used in check_end_of_month_year
else
  day_temp=$day4            # day_temp will be used in check_end_of_month_year
fi
check_end_of_month_year            # check if it will be end of month/year
day4=`printf "%02d" $day_temp`        # day4 = daytemp but with leading zero
FILE4=$year$month$day4"_US.gif"        # real filename for FILE4 download

if [ $new_month = yes ]; then
  day_temp=$[$day_temp+1]        # day_temp will be used in check_end_of_month_year
else
  day_temp=$day5               # day_temp will be used in check_end_of_month_year
fi
check_end_of_month_year            # check if it will be end of month/year
day5=`printf "%02d" $day_temp`        # day5 = daytemp but with leading zero
FILE5=$year$month$day5"_US.gif"        # real filename for FILE5 download

In stead of day1=... day2=.... day3=... etc, I started looking for something like (but did not functionate)

day0=`date +"%e"`
for ((i=1; i<6; i++)); 
do
   day$i=$[$day$(($i-1)) + 1]      # find the value of the previous day and add that value with one
done

Who can help me by shorten the script?

I replaced lot of statements in your script by using for loop.

The script is as follows.

day=`date +"%Y%m%d"`
month=`date +"%m"`
year=`date +"%Y"`

days_in_month()                # maximum days of every month
{
  case $month in
    09|04|06|11) maxdays=30 ;;
    01|03|05|07|08|10|12) maxdays=31 ;;
    02) if (( ("$year" % 400) == "0" )) || (( ("$year" % 4 == "0") && ("$year" % 100 != "0") )) ; then
          maxdays=29
        else
          maxdays=28
       fi
  esac
}

check_end_of_month_year()        # check if it will be end of month/year
{
  if [ $day_temp -gt $maxdays ]; then # reached end of month
    day_temp=01
    month=$[$month+1]
    if [ $month -gt 12 ]; then       # reached and of year
      month=1
      year=$[$year+1]
    fi
    new_month=yes            #
    days_in_month            # calculate max days in new month
  fi
  month=`printf "%02d" $month`
}

days_in_month                   # how many days does current month have
new_month=no                  # parameter to indicate new month or not
day0=`date +"%e"`            # put date of today in day0
FILE0=$day"_US.gif"            # real filename for FILE0 download

# ######################################
# ------- I think the source below can be smaller -------
# ######################################

j=1
for((i=0;i<5;i++))
do
day$j=$[$day$i+1]                # day1 = day of today + 1 so tomorrows date
let j=j+1
done

for((i=1;i<6;i++))
do
if [ $new_month = yes ]; then
day_temp=$[$day_temp+1]        # day_temp will be used in check_end_of_month_year
else
day_temp=$day$i              # day_temp will be used in check_end_of_month_year
fi
check_end_of_month_year            # check if it will be end of month/year
day$i=`printf "%02d" $day_temp`        # day1 = daytemp but with leading zero
FILE$i=$year$month$day$i"_US.gif"        # real filename for FILE1 download
done

Thanks, looks much much better. However, some commands are not recognized at the busybox I use. I tried your source code and it gave errors:

This is not recognized:

  day$j=$[$day$i+1]                # day1 = day of today + 1 so tomorrows date

and returns with

day1=201002261: command not found

So it did not add 1 day to today but put the 1 behind the date of today.
With other words: it should give 27 but it gave: 201002261

In that construction $day$i is seen as two variables: $day and $i that is why the result is: 20100226 and 1; combined to 201002261

Sorry.Now you check the following code.I corrected your errors.

day=`date +"%Y%m%d"`
month=`date +"%m"`
year=`date +"%Y"`

days_in_month()                # maximum days of every month
{
  case $month in
    09|04|06|11) maxdays=30 ;;
    01|03|05|07|08|10|12) maxdays=31 ;;
    02) if (( ("$year" % 400) == "0" )) || (( ("$year" % 4 == "0") && ("$year" % 100 != "0") )) ; then
          maxdays=29
        else
          maxdays=28
       fi
  esac
}

check_end_of_month_year()        # check if it will be end of month/year
{
  if [ $day_temp -gt $maxdays ]; then # reached end of month
    day_temp=01
    month=$[$month+1]
    if [ $month -gt 12 ]; then       # reached and of year
      month=1
      year=$[$year+1]
    fi
    new_month=yes            #
    days_in_month            # calculate max days in new month
  fi
  month=`printf "%02d" $month`
}

days_in_month                   # how many days does current month have
new_month=no                  # parameter to indicate new month or not
day0=`date +"%e"`            # put date of today in day0
FILE0=$day"_US.gif"            # real filename for FILE0 download

# ######################################
# ------- I think the source below can be smaller -------
# ######################################

j=1
for((i=0;i<5;i++))
do
let day$j=$[$day$i+1]                # day1 = day of today + 1 so tomorrows date
let j=j+1
done

for((i=1;i<6;i++))
do
if [ $new_month = yes ]; then
day_temp=$[$day_temp+1]        # day_temp will be used in check_end_of_month_year
else
day_temp=$day$i              # day_temp will be used in check_end_of_month_year
fi
check_end_of_month_year            # check if it will be end of month/year
let day$i=`printf "%02d" $day_temp`        # day1 = daytemp but with leading zero
if [[ $i -eq 1 ]];then
FILE1=$year$month$day$i"_US.gif"        # real filename for FILE1 download
elif [[ $i -eq 2 ]];then
FILE2=$year$month$day$i"_US.gif"        # real filename for FILE2 download
elif [[ $i -eq 3 ]];then
FILE3=$year$month$day$i"_US.gif"        # real filename for FILE3 download
elif [[ $i -eq 4 ]];then
FILE4=$year$month$day$i"_US.gif"        # real filename for FILE4 download
elif [[ $i -eq 5 ]];then
FILE5=$year$month$day$i"_US.gif"        # real filename for FILE5 download
fi
done

Thanks again but same error happens now with the FILE$i variable.

This

FILE1=$year$month$day$i"_US.gif"

give as result:

201003201002261_US.gif

and should be

20100227_US.gif

So again, the $day$i is not recognized well and also let day$j=$[$day$i+1] is not recognized as it should

Sorry.There is some problem in interpolation of the variables.

Now,I corrected those errors.You check the following code.

day=`date +"%Y%m%d"`
month=`date +"%m"`
year=`date +"%Y"`

days_in_month()                # maximum days of every month
{
  case $month in
    09|04|06|11) maxdays=30 ;;
    01|03|05|07|08|10|12) maxdays=31 ;;
    02) if (( ("$year" % 400) == "0" )) || (( ("$year" % 4 == "0") && ("$year" % 100 != "0") )) ; then
          maxdays=29
        else
          maxdays=28
       fi
  esac
}

check_end_of_month_year()        # check if it will be end of month/year
{
  if [ $day_temp -gt $maxdays ]; then # reached end of month
    day_temp=01
    month=$[$month+1]
    if [ $month -gt 12 ]; then       # reached and of year
      month=1
      year=$[$year+1]
    fi
    new_month=yes            #
    days_in_month            # calculate max days in new month
  fi
  month=`printf "%02d" $month`
}

days_in_month                   # how many days does current month have
new_month=no                  # parameter to indicate new month or not
day[0]=`date +"%e"`            # put date of today in day0
FILE0=$day"_US.gif"            # real filename for FILE0 download

# ######################################
# ------- I think the source below can be smaller -------
# ######################################

j=1
for((i=0;i<5;i++))
do
let day[$j]=$[${day[$i]}+1]
let j=j+1
done

for((i=1;i<6;i++))
do
if [ $new_month = yes ]; then
  day_temp=$[$day_temp+1]        # day_temp will be used in check_end_of_month_year
else
  day_temp=${day[$i]}
fi
check_end_of_month_year            # check if it will be end of month/year
day[$i]=`printf "%02d" $day_temp`        # day1 = daytemp but with leading zero

#echo ${day[$i]}

if [[ $i -eq 1 ]];then
FILE1=$year$month${day[$i]}"_US.gif"        # real filename for FILE1 download
#echo $FILE1
elif [[ $i -eq 2 ]];then
FILE2=$year$month${day[$i]}"_US.gif"        # real filename for FILE1 download
#echo $FILE2
elif [[ $i -eq 3 ]];then
FILE3=$year$month${day[$i]}"_US.gif"        # real filename for FILE1 download
#echo $FILE3
elif [[ $i -eq 4 ]];then
FILE4=$year$month${day[$i]}"_US.gif"        # real filename for FILE1 download
#echo $FILE4
elif [[ $i -eq 5 ]];then
FILE5=$year$month${day[$i]}"_US.gif"        # real filename for FILE1 download
#echo $FILE5
fi
done

Wow, thanks a lot it works. I just have changed

  else
    day_temp=${day[$i]}
  fi

into

  else
    day_temp=${day[$i]: -2}        # day_temp is filled with the last 2 characters of ${day[$i]}
  fi