Friendz.. plz help me on this date function.


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Friendz.. plz help me on this date function.
# 15  
Old 07-23-2008
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"
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Plz Help in sorting the data on date basis

I have file a.txt having below data cat a.txt 01-MAY-13 2.38.11.00.100089 IN 4512 0000741881 01-JUN-13 2.38.11.00.100089 IN 1514 0000764631 01-NOV-13 2.38.11.00.100089 IN 1514 0000856571 01-NOV-13 2.38.15.00.100015 IN 300.32 0000856531 01-JUN-13 2.38.19.00.100000 IN 2698 0000764493... (5 Replies)
Discussion started by: ranabhavish
5 Replies

2. UNIX for Dummies Questions & Answers

Plz Help in sorting the data on date basis

I have file having below data 01-MAY-13 2.38.11.00.100089 IN 4512 0000741881 01-JUN-13 2.38.11.00.100089 IN 1514 0000764631 01-NOV-13 2.38.11.00.100089 IN 1514 0000856571 01-NOV-13 2.38.15.00.100015 IN 300.32 0000856531 01-JUN-13 2.38.19.00.100000 IN 2698 0000764493 01-JUL-13... (2 Replies)
Discussion started by: ranabhavish
2 Replies

3. Shell Programming and Scripting

Need to Add date on first position.Plz help

We have a file as shown below with Header and details below Header.Here Header indicates date field. i Want to add date i.e 20100928 as a first position as shown below without header.Please help us using unix script Source file: H20100928 D04041201609360677PC790010384I ... (17 Replies)
Discussion started by: dprakash
17 Replies

4. Shell Programming and Scripting

Need-To remove header/trailor and add-date-on first-postion-plz-help

Hi Friends thanks a lot....for ur replies its worked fine...i have one more question.. if i need to remove trailor (T000000004 ) as shown below file also with header.Plz help thanks Source file: H20100928 D04041201609360677PC790010384I D04041203326357853PC790010645R ... (1 Reply)
Discussion started by: dprakash
1 Replies

5. Shell Programming and Scripting

Date function

I read man page for etc/shadow field.. on the 8th field, i assume that's the field to change account expire date. my question is: What value does the 8th field keep? i assume it's 13514 instead of "Date" value such as 11/10/08. on the man page, it said: " expire value = 13514 = jan 1,... (11 Replies)
Discussion started by: c00kie88
11 Replies

6. Shell Programming and Scripting

Date Function

Hi, My file format is: E102,0,21-04-2007,0,2/25/1994,E003,A,125400,10450,60.2884620 E103,0,21/04/2007,0,2/2/1996,E003,A,125400,10450,60.2884620 E104,0,04/21/2007,0,2/2/1996,E003,A,125400,10450,60.2884620 E105,0,21-APR-2007,0,2/2/1996,E003,A,125400,10450,60.2884620... (1 Reply)
Discussion started by: charandevu
1 Replies

7. Shell Programming and Scripting

Date Function

Hi, My file format is: E102,0,21-04-2007,0,2/25/1994,E003,A,125400,10450,60.2884620 E103,0,21/04/2007,0,2/2/1996,E003,A,125400,10450,60.2884620 E104,0,04/21/2007,0,2/2/1996,E003,A,125400,10450,60.2884620 E105,0,21-APR-2007,0,2/2/1996,E003,A,125400,10450,60.2884620... (1 Reply)
Discussion started by: charandevu
1 Replies

8. Shell Programming and Scripting

date function

hi, I have to ftp previous days file from a directory to another location. The name of the files are like "xxx20060225" (yyyymmdd format) "xxx20060226" ls -lrt xxx*| tail -2| head -1 will give me the file, but if i could get anything... (2 Replies)
Discussion started by: abey
2 Replies

9. UNIX for Dummies Questions & Answers

Date function question

hi guys! just want to ask if you could help me with the sript i'm working on. i need to automatically generate a summarized report everyday for all transactions the day before and ftp it to another machine. my only problem is that i need to name the file as the date yesterday. for example if i... (12 Replies)
Discussion started by: crpalermo
12 Replies

10. UNIX for Dummies Questions & Answers

plz Help How should I configure cc compiler output file plz help???

i.e configuration of C compiler :confused: (4 Replies)
Discussion started by: atiato
4 Replies
Login or Register to Ask a Question