Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Shell script to get previous date 6

Status
Not open for further replies.

babeo

Technical User
Mar 30, 2000
398
0
0
CA
Hi
I write a script to create file with name of previous date, but get stuck on how to get the previous date:
#!/bin/sh
date +%d > TODAY
TODAY -=TODAY #this is I want to make previous day
touch myserver>mydir/myfile.TODAY1200
--------

you can see line 2, I just want to get the date only to be stored in TODAY file, then I get stuck on the next line.
Is there any command to get the previous date or could someone figures out my terrible script please.

Thanks.

 
First off, the statement "date +%d > TODAY" pipes the output of the date command to a file named TODAY. Hence the statement(s) following would not work. What you need to do is convert the TODAY variable to an expression (it's then a number) after which you can apply arithmetic operands to it. Try the following, it should work:

TODAY=`date +%d`
TODAY=`expr $TODAY - 1`
echo $TODAY

Hope this helps.

- Stuart
 
Wow, thanks

It works finally, however, it does not work properly !!
here is my result:

myfile.10101

The script is ran today as of Jan 02, 2001, and I expect to have file created as myfile.010101, but the zero of the day is missing, and I need that zero, how can I fix that problem ? (I think if the day is either 1x or 2x or 3x then I don't have any problem of leading zero, but for days from 1-9, I have leading zero and it is the problem.)

Thanks a lot
 
Seems good in principle, but what if you run on the 1st of June say?, the expression won't return 31 (for May), but zero. What I do/have done is either ...

1. Use my database server to return a date in whatever format I specify (my preferred method nowadays)

or

2. Maintain a file called yesterday in a specified directory containing the date I need. Then at 11:59pm every night, I write the date to it (using a small script running in cron). So next day, if I need the previous days date, I just "cat" the contents of the file, using something like

FILENAME=myfile.`cat /home/yesterday`

Unfortunately Unix (on it's own) is not of very much use when it comes to date calculations.

Greg.
 
Thanks Greg for your figure out. You're right.

I think I will try your way.
Thank again all
 
TODAY=`date "+%y %m %d" | nawk '{split("31,31,28,31,30,31,30,31,31,30,31,30",month,","); if ($1 % 4 == 0) {month[3]=29;}; day=$3-1; if (day == 0) {day=month[$2-0];} printf("%02d",day);}'`
echo $TODAY

yeah i know ... but it works :)
 
It does jad, but it makes my head hurt! :-\ .. lol

Greg.
 
How would you subtract multiple days from the present date.

I need to generate two dates, but I need to use the complete date (01/03/01)

$varible1 = present date
$varibel2 = date - 14 days

How might this be accomplished ?



-Danny






 
TODAY=`date &quot;+%y %m %d&quot; | nawk '{split(&quot;31,31,28,31,30,31,30,31,31,30,31,30&quot;,month,&quot;,&quot;); if ($1 % 4 == 0) {month[3]=29;}; day=$3-<num days>; year=$1; mon=$2; if (day <= 0) {day+=month[$2-0]; mon=$2-1; year=$1-1} printf(&quot;%02d/%02d/%02d&quot;,day,mon,year);}'`
echo $TODAY

woo :)

i love scripts

note <num days> is the number of days you want to subtract :)
 
jad,

looks very promising

However I am getting an error when I try to use your script.
I replaced <num days> with 14 and this is what happensnawk: Syntax error
at line 1 of program << {split(&quot;31,31,28,31, ... >>
context is
if ($1 % 4 == 0) {month[3]=29;}; day=$3-14 >>> year= <<< $1; mon=$2;
nawk: illegal statement
at line 1 of program << {split(&quot;31,31,28,31, ... >>

Any advise



-Danny






 
TODAY=`date &quot;+%y %m %d&quot; | nawk '{split(&quot;31,31,28,31,30,31,30,31,31,30,31,30&quot;,month,&quot;,&quot;); if ($1 % 4 == 0) {month[3]=29;}; day=$3-14; year=$1+0; mon=$2+0; if (day <= 0) {day+=month[$2-0]; mon=$2-1; if (mon==0) {year=$1-1; mon=12;}} printf(&quot;%02d/%02d/%02d&quot;,day,mon,year);}'`
echo $TODAY

i got the year slightly wrong anyway ... but the stuff above works (forgot to convert from string to number with +0)
 
jad,

That's a beautiful thing.

You deserve a pint of Guiness for that one.

It works wonderful !!!!!!!!!

thank you


-Danny






 
if it weren't for the new baby, and wanting to avoid the hangover, i'd take you up on that one :)

Jon
 
TODAY=`date &quot;+%y %m %d&quot; | nawk '{split(&quot;31,31,28,31,30,31,30,31,31,30,31,30&quot;,month,&quot;,&quot;); if ($1 % 4 == 0) {month[3]=29;}; day=$3-1; if (day == 0) {day=month[$2-0];} printf(&quot;%02d&quot;,day);}'`
echo $TODAY

This works great but I need to add the Month to this too.

Roxanne
 
Hi Roxanne

The code is just there, try this one:

TODAY=`date &quot;+%y %m %d&quot; | nawk '{split(&quot;31,31,28,31,30,31,30,31,31,30,31,30&quot;,month,&quot;,&quot;);
if ($1 % 4 == 0) {month[3]=29;};
day=$3-1;
mon=$2+0;
year=$1+0;[/red]
if (day == 0) {day=month[$2-0];} printf(&quot;%02d/%02d/%02d[/red]&quot;,day,mon, year[/red]);}'`
echo $TODAY

If you don't want the year to be displayed, then just take off the code for the year.
babeo
 
you can also try this:

env TZ=&quot;$TZ+24&quot; date +%d

for same cases is more simple.

Regards,

Carlos Almeida,
 
Month cal is not coming up right.
for Jan, it becomes 00, insted of 12
please help
 
An easy solution to this ....

Set up a cron which runs at five minutes to midnight

date > /tmp/yesterdays_date

When you run your script the next day just cat this file and you have yesterdays date !!!!

It works every time....................
 
it would, but what if cron fails for some reason ?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top