Operating System - Linux
1827881 Members
1178 Online
109969 Solutions
New Discussion

Script with problem for getting dates

 
SOLVED
Go to solution
Rita Li
Frequent Advisor

Script with problem for getting dates

Hi,

Today is Oct-1-2007, a Monday date. My script runs werid that is to start with getting the last Sunday date, then the Monday date before this Sunday date.

day1=`date "+%Y%m%d"`
day2=`date +%w`
if [ $day2 -eq 0 ]; then
sun_day=$day1
else
sun_day=`date +%Y%m%d-$day2|bc`
fi
mon_day=`date +%Y%m%d-$day2-6|bc`
echo $sun_day $mon_day

The result produced is:

20071000 20070994

Pls render help

Rita
3 REPLIES 3
Steven Schweda
Honored Contributor
Solution

Re: Script with problem for getting dates

What's weird? 20071001 - 1 = 20071000, and
20071001 - 1 - 6 = 20070994. You got what
you asked for.

The result of "date '+%Y%m%d'" is not an
ordinary decimal number, so you can't do
ordinary decimal arithmetic on it and get
a resonable answer, except by luck.

You might look at the suggestions for a
similar problem at:

http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=1040167
TY 007
Honored Contributor

Re: Script with problem for getting dates

Hello Rita,

date '+%m %d %Y' |
{
read MONTH DAY YEAR
DAY=`expr "$DAY" - 1`
case "$DAY" in
0)
MONTH=`expr "$MONTH" - 1`
case "$MONTH" in
0)
MONTH=12
YEAR=`expr "$YEAR" - 1`
;;
esac
DAY=`cal $MONTH $YEAR | grep . | fmt -1 | tail -1`
esac
sun_day=$YEAR/$MONTH/$DAY
mon_day=`date +%Y/%m/%d`
echo $sun_day $mon_day
}

The result produced is:

2007/9/30 2007/10/01

Please modify above script further.

Thanks

Rita Li
Frequent Advisor

Re: Script with problem for getting dates

A. Clay's script is great

Problem is fixed

Thank you for the prompt respond

Rita