trouble with script

I'm having a little trouble finishing up this script any help would be great.

My system is SCO OpenServer Enterprise System (ver 5.0.5m) and i'm using sh

This script checks todays date and goes and downloads a file with yesterdays date in the name.

---start----
Server="ipaddresshere"
usern="usernamehere"
pword="passwordhere"
day=`eval date +%Y%m`
tmp1=`eval date +%d`
num=`eval echo $tmp1 - 1 | bc`
tmp2=$day$num
file=filename_$tmp2.txt
cd /tmp
ftp -i -n $Server <<here
user $usern $pword
binary
cd folder1
cd folder2
get $file
quit
here
exit
---end----
Now my problem is what happens on the first of the month... how do i make this script use the 30th or the 31th on the first of the month.

Any help would be great.

Thanks.

whegra,

I believe there are versions of "date" out there that provide date subtraction functionality, but here's a thought:

YES_DAY=0
TODAY=$(date +%d)

if [ ${TODAY} = 01 ]
then
[ ${MONTH} = "01" ] && YES_DAY=31
[ ${MONTH} = "02" ] && YES_DAY=31
[ ${MONTH} = "03" ] && YES_DAY=28
[ ${MONTH} = "04" ] && YES_DAY=31
[ ${MONTH} = "05" ] && YES_DAY=30
[ ${MONTH} = "06" ] && YES_DAY=31
[ ${MONTH} = "07" ] && YES_DAY=30
[ ${MONTH} = "08" ] && YES_DAY=31
[ ${MONTH} = "09" ] && YES_DAY=31
[ ${MONTH} = "10" ] && YES_DAY=30
[ ${MONTH} = "11" ] && YES_DAY=31
[ ${MONTH} = "12" ] && YES_DAY=30
fi

There may be an easier way, but I thought I'd toss this out there to get you started. You may also want to modify the above to account for leap years.

You can check out the following page for some more ideas/scripts:

http://unix.about.com/library/weekly/aa070901a.htm

Hope this helps!

Biker
Systems/Network Administrator
LiveFire Labs - Hands-On Technical e-Learning

P.S. Using a case statement in place of the multiple tests will reduce the amount of code needed.

Biker
Systems/Network Administrator
LiveFire Labs - Hands-On Technical e-Learning

I posted a script that can handle date arithmetic. You can find it in this post.

Thanks Guys,

That was exactly what I needed.