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
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
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
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