Problem to get yesterday's date flatfile with shell script

hi, i was required to write a shell script to get yesterday's date flatfile. but i only know how to get today's date flatfile. Please observed my below scripting: Please help! Thanks

=================================================

#!/bin/sh
HOST='192.168.1.200'
USER='ftp1'
PASSWD='1234dfr'
DIR='/rpt/dailyflatfile'
LDIR='/accmgt_inbox'
FILE='p2p'
DATE=`date '+%Y%m%d'`
 
ftp -n $HOST <<END_SCRIPT
quote USER $USER
quote PASS $PASSWD
lcd $LDIR
bin
prompt
mget $FILE$DATE
quit
END_SCRIPT
exit 0

=======================================================

do you have GNU date??

Hi, sorry i'm not familiar with shell script, do i need to install it?
Thanks!

To get yesterday date with date:

date --date="yesterday" '+%Y%m%d'

in your script try:

DATE=`date --date="yesterday" '+%Y%m%d'`

hi,

try using below code.

Ydate=`date '+%y:%m:%d' | awk -F":" '{printf"20%2d%2d%2d\n",$1,$2,($3-1)}' | sed 's/ /0/g'`

On the first day of the month it will return 00

Hi Phil,

i had tried your script, however it failed with date error
Thanks!

---------- Post updated at 04:41 PM ---------- Previous update was at 04:35 PM ----------

Hi Sandeep909,

I tried your script, it works well, i managed to mget yesterday's date flatfile.
But is there any exception like On the first day of the month it will return 00
Thanks Bro!

what does your system give you when you type:

date --date="yesterday" '+%Y%m%d'

and/or

DATE=$(date -d yesterday +"%Y%m%d")
echo $DATE

?

the command

date

GNU at least on 6.9 works with the --date switch
it might help if you give us the result of:

man date | grep GNU

Hello!

Per forum rules, and the benefit of all users, please search the forums before posting a question.

You can easily search the forums using our internal Google search engine or our advanced internal search engine. You can also search our huge UNIX and Linux database by user generated tags or search the database for unanswered questions and provide an answer.

Search for yesterday date.

Thank you.

The UNIX and Linux Forums

hi,
i got this error when trying date --date="yesterday" '+%Y%m%d':

date: illegal option -- date=yesterday
usage: date [-u] mmddHHMM[[cc]yy][.SS]
date [-u] [+format]
date -a [-]sss[.fff]

hi lifeseries,

how current date displays when you type date command.

you can use TZ..

Hi Sandeep,

my current date display is "Tue Oct 6 11:21:12 MYT 2009"

sorry i'm not familiar with TZ, can you guide me on how to code it?
Thanks.

hi,

so now you can use as below code...

YDAY=`TZ=MYT+16 date '+%y%m%d%'`

Hi Sandeep,

appreciate if you can explain what is the meaning of TZ=MYT+16?
will it permits overriding the default time zone? i just scare it override default time zone, thanks.

Also i'm running this script everyday through cronjob, will it be any issue?

TZ means time zone.... and MYT is your local time.. 16 is difference from GMT to get yesterday date...
and your are taking it into variable so it would not override default time zone.

you can run in cron also but i am not very familier with cron.

Hi
do i need to change the 16 value every year?
Thanks!

---------- Post updated 10-07-09 at 10:24 AM ---------- Previous update was 10-06-09 at 02:17 PM ----------

Hi Sandeep,

i will get the flatfile from my server to another server which reside in my country also, so do i need to follow GMT?
let say i run the cronjob at oct 10 at 4 am, can i set the TZ=MYT+6, which is oct 9?
Thanks!

hi,

you can use the TZ variable to match your server timings.. but if you want exactly the same time and date yesterday, you have to use TZ=MYT+16..

if i am not wrong MYT is malaysian time... if it is GMT+8hrs... now to get Ydate you need to use MYT+(24-8).

jus send the output of "date" command and "echo `TZ=MYT+16 date`"

if you directly use TZ=MYT+16 date at prompt , your default time will be override because TZ is a environmental variable. so better to take into some temp variable.

i hope i was clarified.

Hi Sandeep,

i got 1 more last question, my shell script as below: If i run it thru cronjob everyday, will it override my default time zone? Also what is mean by temp variable?
Thanks!

#!/bin/sh
HOST='192.100.93.12'
USER='user'
PASSWD='password'
LDIR='/PROD'
FILE='accmgt'
YDAY=`TZ=MYT+12 date '+%Y%m%d'`

ftp -n $HOST <<END_SCRIPT
quote USER $USER
quote PASS $PASSWD
lcd $LDIR
bin
prompt
mget $FILE$YDAY*.txt
quit
END_SCRIPT
exit 0

hi,

what i mean by temp variable is nothing but YDAY in your script.. and your default time zone will not be override as your are using now.

and in script ftp block might not work.. Replace with below block and try

 
ftp -inv $HOST <<END_SCRIPT
user $USER $PASSWD
cd $LDIR
bin
prompt
mget $FILE$YDAY*.txt
bye
END_SCRIPT
date '+%d%m%Y' --date '1 day ago'