A newbie with a problem in A date Script

Hello everybody...
I'm a Unix newbie and i just got this task at work to figure out what's wrong with a daily script my team is using.

The idea behind the script is that it takes the day before in a yyyymmdd format, find files with that date in a specific directory and executes an (irrelavant) operation on them.
The problem with the script is that it won't work in every 1st of every month. So in every 1st of each month they need to change the script manualy.
I think that it has to do with the $d-1 section so in eache 1st of the month it thinks that the day before is 0.
My task is to figure out a way to change the script so it will work every day of the month including the 1st of every month.
:confused:
It's my first day on a new job and i dont know anything about scripting so please be gentle. I'd appreciate it if you could find a way to correct it and explain it to me.
The relevant lines are these:

d=`date '+%d'`
d=`/usr/bin/expr $d - 1`
if [ $d -lt 10 ]
then
DATE=`date '+%Y%m'`0$d
else
DATE=`date '+%Y%m'`$d
fi
DATE='20060831'
cd ${root_dir}
num=`ls -1 dec_${DATE}*|wc -l`>/dev/null
if [ $num -lt 21 ]
#if [ $num -lt 1 ]
then

Plz help...
Thx in advance !!!!

Which OS do you use?

for freebsd you can replace

by d=`date -v -1d +%d`

Hi,

Please see this link

http://www.unix.com/showthread.php?t=29685

I had discovered a small bug in my code later. Here's the fully updated code. It takes care of previous month, previous year, leap year issues while calculating yesterday's date.

#! /bin/sh
###########################################################################################
## This scripts runs in 2 ways                                                            #
## First way: without command line arguments. It calculates yesterday's date              #
## Second way: 3 numeric argumnets(dd mm yyyy). Calculates date prior to the input date   #
###########################################################################################

case $# in
3)
   [[ `echo $1 | grep [[:alpha:]]` || `echo $1 | grep [[:punct:]]` ]] && echo -e "$1 is not a number.\nQuitting" && exit 1
   [[ `echo $2 | grep [[:alpha:]]` || `echo $2 | grep [[:punct:]]` ]] && echo -e "$2 is not a number.\nQuitting" && exit 1
   [[ `echo $3 | grep [[:alpha:]]` || `echo $3 | grep [[:punct:]]` ]] && echo -e "$3 is not a number.\nQuitting" && exit 1

   YEAR=$3
   NMONTH=$2
   DATE=$1
   # Add a 0 in front if month or date is single digit; i.e. < 10
   [[ ${#NMONTH} -eq 1 ]] && NMONTH="0$NMONTH"
   [[ ${#DATE} -eq 1 ]] && DATE="0$DATE"

   [[ $DATE > 31 || $DATE < 01 ]] && echo -e "Invalid date.\n Quitting" && exit 1
   [[ $DATE > 30 && ($NMONTH == 04 || $NMONTH == 06 || $NMONTH == 09 || $NMONTH == 11) ]] && echo -e "Invalid date.\n Quitting" && exit 1
   [[ $NMONTH > 12 || $NMONTH < 01 ]] && echo -e "Invalid month.\n Quitting" && exit 1
   [[ ${#YEAR} -ne 4 ]] && echo -e "Invalid Year.\n Quitting" && exit 1

   if [[ $NMONTH == 02 && $DATE > 28 ]]; then # Month is February, no. of days greater than 28
        if [[ `expr $YEAR % 4` != 0 ]]; then # If not divisible by 4, can't be a leap year
             echo -e "Invalid date. \n Quitting" && exit 1
        else # If divisible by 100 but not divisible by 400, can't be a leap year
           [[ `expr $YEAR % 100` == 0 && `expr $YEAR % 400` != 0 ]] && echo -e "Invalid date. \n Quitting" && exit 1
        fi
        [[ $DATE > 29 ]] && echo -e "Invalid date. \n Quitting" && exit 1
   fi

   ;;
*)
YEAR=`date +"%Y"`
NMONTH=`date +"%m"`
DATE=`date +"%d"`;;
esac
if [[ $DATE == 01 || $DATE == 1 ]]; then # If its the first day of the month, set yesterday's date accordingly.

case $NMONTH in
01|02|04|06|08|09|11)
        DATE=31
        if [[ $NMONTH == "01" || $NMONTH == "1" ]]; then
                YEAR=`expr $YEAR - 1`
                NMONTH=12;LMONTH="December"
        else
         NMONTH=`expr $NMONTH - 1`
        fi;;


03)     if [[ `expr $YEAR % 4` == 0 ]]; then #Year divisible by 4
                if [[ `expr $YEAR % 100` == 0 ]]; then #YEAR divisible by 100
                        DATE=29
                        if [[ `expr $YEAR % 400` != 0 ]]; then #YEAR not divisible by 400 is not a leap year
                                DATE=28
                        fi
                else
                     DATE=29
                fi
        else
                DATE=28
        fi
        NMONTH=`expr $NMONTH - 1`;;
*)
        DATE=30
        NMONTH=`expr $NMONTH - 1` ;;
esac

else
       DATE=`expr $DATE - 1`
fi

# If numeric month or date is <10, add a 0 in front
if [ ${#NMONTH} -eq 1 ]; then
  NMONTH="0$NMONTH"
fi
if [ ${#DATE} -eq 1 ]; then
 DATE="0$DATE"
fi

case $NMONTH in # set long month name
01)     LMONTH="January";;
02)     LMONTH="February";;
03)     LMONTH="March";;
04)     LMONTH="April";;
05)     LMONTH="May";;
06)     LMONTH="June";;
07)     LMONTH="July";;
08)     LMONTH="August";;
09)     LMONTH="September";;
10)     LMONTH="October";;
11)     LMONTH="November";;
12)     LMONTH="December";;
esac


echo $DATE-$NMONTH-$YEAR-$LMONTH #Output date-month-year

Some changes will be required to make it accept yyyymmdd date as input. currently it expects dd mm yyyy as input.

[offtop]
horror
[/offtop]

Your help is appreciated !!
Thanx !!!