![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| UNIX for Advanced & Expert Users Advanced UNIX and Linux questions go here. Expert-to-Expert. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#15
|
|||
|
|||
|
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" |
|||
| Google The UNIX and Linux Forums |
| Forum Sponsor | ||
|
|