Operating System - Linux
1819915 Members
2274 Online
109607 Solutions
New Discussion юеВ

Re: How get a date in file a day after?

 
debian111
Regular Advisor

How get a date in file a day after?

Hallo everybody,

I am trying to write some script which will sort some data using awk. In that script I need a date for every day, and it should be changed for that day.

I tried to acomplish this using
echo '+%Y%m%d'>date
d='cat date'

This script should write some data on day basis and it should do it automatically.
I other words when I run this script now, inside it I need come mechanism to get a date for yesterday. I will make it to run every day at 03:00 AM for previous day, so it is necessary to be able to get a date for that day, and it should be a variable.

I think this is probably messy, but every hint related to this is welcomed.

Regards
9 REPLIES 9
debian111
Regular Advisor

Re: How get a date in file a day after?

I used this
d=`date +%Y%m%d`

and added to A_$d.txt to get a filename for day in form A_20060824.txt, but I cannot use this because I need to run this script after 00:00 for previous day and above statement related to date is not valid in that case.

So is there any way to add date for yesterday in script which I run today.


Thanks
Ivan Ferreira
Honored Contributor

Re: How get a date in file a day after?

You can use date --date, for example:

date --date yesterday +%Y%m%d

Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
debian111
Regular Advisor

Re: How get a date in file a day after?

Thanks Ivan,

your solution works on my linux machines, but in case of Siemens Reliant UNIX 5.43 it does not work.
It is interesting that I need this the most on this Relinat Unix machine. I read man page for date on Relinat Unix machine, but there is not any hint how set time for this purpose.

Thanks again.

Regards
spex
Honored Contributor

Re: How get a date in file a day after?

Hi,

On my systems, I have this cronjob set up:

59 23 * * * /usr/bin/date > /tmp/yester # Yesterday's date to /tmp/yester

Then anytime I need yesterday's date, I just reference /tmp/yester.

PCS
Huc_1
Honored Contributor

Re: How get a date in file a day after?

I would be carefull of putting this in crontab as in the previous reply... this will work most of the time I am sure, but ... what if system is very loaded and crontab run one nimute later?.

what if you need this during last minute of the day,... perhaps you could set this for last second of the day but then the risk it run on next day gets high!

Perhaps it is posible to do a test like
ls -l /tmp/yester and check this with the containt of /tmp/yester to see is this did run on the day before or passed the day line !

just less then 2 cent worth rumbling

Jean-Pierre Huc
Smile I will feel the difference
spex
Honored Contributor

Re: How get a date in file a day after?

Huc,

Of course, you are right: care must be taken with this approach. However, it really is quite reliable as long as you keep those caveats in mind. And it can be made even safer by verifying the timestamp, as you suggested.

PCS
Huc_1
Honored Contributor

Re: How get a date in file a day after?

Still rumbling on this issue, an other ideas I had, and that you could posibly use is perl if you have perl on both OS ... you could then use something like
Date::Manip

Have a look at man Date::Manip lots of examples there.

Hope this is usable for you.

Jean-Pierre Huc

Smile I will feel the difference
debian111
Regular Advisor

Re: How get a date in file a day after?

Thanks for answers, I made it like bellow

d=`TZ=MET+24 date +%Y%m%d`

and I use d as variable.

For two days ago
d=`TZ=MET+48 date +%Y%m%d`

and so on.

Regards
debian111
Regular Advisor

Re: How get a date in file a day after?

Thanks again,

I think forums like this one are very helpfull for people who works with different technologies and all of us have to support places like this.

Thanks