Date function question

hi guys!
just want to ask if you could help me with the sript i'm working on. i need to automatically generate a summarized report everyday for all transactions the day before and ftp it to another machine. my only problem is that i need to name the file as the date yesterday. for example if i would generate a report today (09172001), the filename should be 09162001.txt, i've read the man for date but i haven't got a clue as to what to do.
please help.
thanks!

Dear friend

write a few lines in the shell as follows:

bye using date format command store today's date value in variable dt

hence if todays date is 09282001 in mmddyyyy format
dt='09282001'
now store value of dt to a file
echo $dt>file1
now cut value of today's date in variable v1
v1=`cut -c3-4 file1`
now find out value of previous date by using expr command
v2=`expr $v1 -1`
now prepare the full date by replacing 3rd and 4th chr of file1 by value of v1

Of course you will also have to add validations for last date of months and for february also
hope expr command will help you

Wish you all the best

yours friendly
VRJoshi

just a thought. I dont see much use of processing data and labeling it the day befor. why not just run it from cron @ end of bussiness day or even at 11:55pm then u dont have to worry about adding and subtracting from the date.?

also if you look at the date man page at the bottom there are plenty of examples of getting the date in a format you would like.

Then what if something happens at 11:56, that wont be reflected in the log file. One solution might be to do something like:

DATESTRING=`date +%m%d%Y`

sleep 120

# run report, redirect to $DATESTRING.txt

Run it at 11:59pm from crontab...

ptx: i agree but that is why i said run it late.ie 11:56. most business should be done.

but w/ your example i completly agree and it is a great idea to get the date string first then sleep for 2 mins then finish.

Well, I'm bored this afternoon...so

#!/usr/bin/ksh

set -A months 0 31 28 31 30 31 30 31 31 30 31 30 31
typeset -Z2 month_y day_y

today=$(date +%m%d%Y)

month_t=${today%??????}
year_t=${today#????}
day_t=${today%????}
day_t=${day_t#??}

((year_t % 100)) && ((leap = !(year_t % 4))) || ((leap = !(year_t % 400)))
((leap)) && ((months[2] = months[2] + 1))

((year_y = year_t))
((month_y = month_t))
((day_y = day_t - 1))

if ((!day_y)) ; then
   ((month_y = month_y - 1))
   if ((!month_y)) ; then
      ((year_y = year_y-1))
      ((month_y = 12))
   fi
   ((day_y = months[month_y]))
fi

yesterday=${month_y}${day_y}${year_y}
echo yesterday = $yesterday
	
exit 0

I think I got this right...I only tested it lightly.

Thanks for all the help guys! I highly appreciate it

Hi,

sorry for the delayed response, just figured out.

u can use:

echo `echo '*time-0t86400=Y' | /usr/bin/adb -k | tail -2`

to get yesterdays date.

Thanks,

Whoa! Interesting approach, kandanmv! And I didn't know adb defaults the objectfile and corefile with -k like that. But it does seem to work.

gee thanks.
but the easiest way would be to use perl

# perl -e 'printf "%s", scalar localtime(time-86400)'

regds,

Or save yourself a few bytes:

perl -e 'print scalar localtime(time-86400)'

Thanks, kandanmv and PxT. The Perl commands are useful.

Perderabo's code was very helpful. I used it adn worked fine, except that my linux box was giving me errors on set and typset commands:confused:, so I just took them out!
Thanks
Nitin :smiley: