Sent via Deja.com http://www.deja.com/
Before you buy.
Summary: you can't do it easily portably and accurately just using the shell,
it's easy using gnu date or mktime and some other interesting hacks exist
for doing it.
Some bad methods include messing with the TZ variable (24 hours ago isn't always
yesterday if your country uses summertime) and the one you posted of creating a
yesterday file in cron (them machine may have been down for three days).
All of this and more will come up in the threads you find.
Hope this helps
Adam Price
>Summary: you can't do it easily portably and accurately just using the shell,
I guess that depends on your definition of "easily". Here's my quick
implementation:
#! /bin/sh
year=`date +%Y`
month=`date +%m`
day=`date +%d`
# figure out what yesterday was.
day=`expr "$day" - 1`
case "$day" in
0)
month=`expr "$month" - 1`
case "$month" in
0)
month=12
year=`expr "$year" - 1`
;;
esac
case "$month" in
1|3|5|7|8|10|12)
day=31
;;
4|6|9|11)
day=30
;;
2)
if [ `expr "$year" % 4` = 0 \
-a `expr "$year" % 100` != 0 \
-o `expr "$year" % 400` = 0 ]
then
day=29
else
day=28
fi
;;
esac
;;
esac
echo "Year $year month $month day $day"
Formatting the results into YYMMDD format is left as an exercise. I
would probably do it with "sed", personally.
Also note that this Bourne shell, not Korn shell. With Korn shell, you
could probably do a cleaner implementation, or at least one that
invokes fewer external programs, although this rarely ever invokes more
than one external program besides "date".
- Logan
In article <8lv3r5$2cv$1...@provolone.cs.utexas.edu>,
Oh, I just realized that my simple implmentation was far too
complicated. What was I thinking? The Unix philosophy is to combine
tools together to get what you need, and there's already a tool that
solves the most difficult part of the problem for you: finding the last
day of a given month. It's called "cal". Well, O.K., it doesn't quite
tell you directly. You have to ask it nicely:
cal 2 2000 | grep . | fmt -1 | tail -1
So anyway, the *simple* way to do it is this:
#! /bin/sh
year=`date +%Y`
month=`date +%m`
day=`date +%d`
# figure out what yesterday was.
day=`expr "$day" - 1`
case "$day" in
0)
month=`expr "$month" - 1`
case "$month" in
0)
month=12
year=`expr "$year" - 1`
;;
esac
# figure out the last day of the month
day=`cal $month $year | grep . | fmt -1 | tail -1`
esac
echo "Year $year month $month day $day"
Actually, this is less efficient than my previous solution because it
has to run the external programs "cal" and "grep" and "fmt" and "tail"
and "expr", whereas the previous one only had to run "expr". But it's
certainly short and easy.
- Logan
bah. it's fairly easy to put in a check for that if you care about that
sort of thing. Just get the "old" value, and see if it is the same as the
current value. If it is the same, then add 1 to 24 and try again.
I more often just want to fake a timestamp for (approximately) 24 hours ago
so I use the TZ method.
--
[Trim the no-bots from my address to reply to me by email!]
[ Do NOT email-CC me on posts. Pick one or the other.]
S.1618 http://thomas.loc.gov/cgi-bin/bdquery/z?d105:SN01618:@@@D
The word of the day is mispergitude