Simpler Navigation for Servers and Operating Systems - Please Update Your Bookmarks
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
If you have bookmarked forums or discussion boards in Servers and Operating Systems, we suggest you check and update them as needed.
Languages and Scripting
cancel
Showing results for 
Search instead for 
Did you mean: 

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
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

Highlighted
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