The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
Google UNIX.COM


UNIX for Advanced & Expert Users Advanced UNIX and Linux questions go here. Expert-to-Expert.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Function to find day of any given date. RRVARMA Shell Programming and Scripting 5 05-12-2008 12:19 AM
Date Function charandevu Shell Programming and Scripting 1 04-02-2008 06:12 AM
Date Function charandevu Shell Programming and Scripting 1 04-02-2008 04:44 AM
Wrong date function Asteroid Shell Programming and Scripting 3 04-04-2007 01:09 AM
date function abey Shell Programming and Scripting 2 02-27-2006 02:28 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #15  
Old 07-23-2008
Registered User
 

Join Date: Sep 2007
Posts: 8
I came up with a potential solution to find the previous Saturday's date based on current system date.

I made use of the existing logic to find previous day (posted on this forum) and based on the current day of the week, I keep looping and calling the previous_day() function until I get to previous Saturday's date.

Please let me know if you find any flaws with my script.

Thanks.

Code follows:

==========================================================
#!/usr/bin/ksh
#
####################################################
# Script to find previous Saturday's date based on current system date
####################################################

# Initialize variable
i=0

#
# Function area - copied from forum posting
#
previous_day()
{
# Convert variables into numeric fields
YEAR=`expr "$YEAR" + 0`
MONTH=`expr "$MONTH" + 0`
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

# If DAY less than 10, append zero in front
if [ ${DAY} -lt 10 ]
then
DAY=0${DAY}
fi

# If MONTH less than 10, append zero in front
if [ ${MONTH} -lt 10 ]
then
MONTH=0${MONTH}
fi

} # End of previous_day() function


# Obtain current system date
YEAR=`date '+%Y'`
MONTH=`date '+%m'`
DAY=`date '+%d'`

# Find current day of the week
wkday=`date '+%w'`
wkday=`expr "$wkday" + 1`

# Loop and call function previous_day until we get previous Saturday's date
while [ i -lt ${wkday} ]
do
# Call function to find previous day
previous_day

let i=i+1
done


# Return yesterday date
echo "$YEAR$MONTH$DAY"
Reply With Quote
Google The UNIX and Linux Forums
Forum Sponsor
Reply

Tags
date, datecalc, linux, shell script, solaris

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 09:28 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0