Sponsored Content
Top Forums Shell Programming and Scripting Check if a day eg: saturday is the Last saturday of the month! Post 302100613 by Andrek on Thursday 21st of December 2006 01:23:20 AM
Old 12-21-2006
I had the same problem but need to get the details if the day was the first or the last for different days.

hence I wrote a script to do the work with options...
Its a little long but I had time to spare when writting it....

heres a "cut-&-paste of the script.

Code:
#! /bin/sh
#
# Script to fnind the last or first given day of the current month
## 
# Andre Kokot 9 Nov 2006
# Started the code
# 
# #####################################################################

Usage() {
echo "`basename $0` -d [0-6] -o [f|l] "
echo
echo "\t-d [0-6] (Sunday-Saturday), Day of Week"
echo "\t-o [f|l] (First|Last),      Day of Month"
echo

exit 99
return
}
# ####################################################################

Defaults() {

tmpfile=/tmp/calfile
TODAY=`date +%d | sed s.^0..`

#Create a calander in file to process
cal `date +%m" "%Y` | grep -v -e '^$' -e `date +%B` -e  Tu > $tmpfile

return
}

# ####################################################################
ConfDay() {

case $2 in
 [0-6]) : ;;
   *) echo "Invalid day"
      Usage ;;
esac

case $1 in
  l) nday=`expr $2 + 1`
   ;;
  f) set -A farry 7 6 5 4 3 2 1
     nday=${farry[$2]}
   ;;
esac


return
}
# ####################################################################
Getinfo() {
#1 - Option
#2 - Date arry from cal

set -A dtarry $2

case $1 in
   f) if [ ${#dtarry[*]} -ge $nday ]
		 then
         nday=`expr ${#dtarry[*]} - $nday`
         dtfound=${dtarry[${nday}]}
        else
         Find $1 2   # Need the previous week details
      fi
      ;;
   l) if [ ${#dtarry[*]} -ge $nday ]      # Its with in current range
       then
         dtfound=${dtarry[${day}]}
       else
         Find $1 2   # Need the previous week details
      fi
      ;;
esac

ShowResult $dtfound

return
}
# ####################################################################
ShowResult() {

case $1 in
   $TODAY)
      EOM=0 ;;  # This means YES
   *)
      EOM=1 ;;  # This means NO
esac

echo "$EOM"
exit $EOM

return
}
# ####################################################################
Find() {

#1 - Option
#2 - head value

case $1 in 
  l) daterow="`cat $tmpfile | sort -r | head -$2 | tail -1`"
     ;;
  f) daterow="`cat $tmpfile | head -$2 | tail -1`"
     ;;
  *) echo
     echo "Can only find (l)ast or (f)irst day"
     echo 
     Usage ;;
esac

Getinfo $1 "$daterow"

return
}
# ####################################################################
# MAIN - STARTS HERE

while getopts d:o: opt
do
  case $opt in
     d) day=$OPTARG
        #day=`echo $OPTARG | tr -d \"[A-z]\"`
		 #if [ $day -gt 6 ] || [ -z "$day" ]
		 #  then
		 #     Usage
		 #fi
		 ;;
     o) option=$OPTARG ;;
     *) Usage ;;
  esac
done

# Set defaults
Defaults
ConfDay $option $day
Find $option 1   # First pass

#
## Now we know what the last day of the month is,
## need to verify if its today
#


Last edited by reborg; 12-21-2006 at 06:59 AM..
 

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Date - Last Sunday thru Saturday

Hi All, I have to kick off a script on every Monday to get some data from database for last week (Sunday thru Saturday). If its Monday (12/12/2005) Begin date will be Sunday - 12/4/2005 End date will be Saturday - 12/10/2005 The script might not kick off on some Mondays. So my... (2 Replies)
Discussion started by: skymirror
2 Replies

2. Solaris

How to check for Saturday or Sunday

Hi , I have a date parameter passed in YYYYMMDD format , how can I check whether it is Sat or Sun on Solaris box , as we can do the same easily on linux box by using date -d YYYYMMDD '+a' . Any pointres will be really helpful . (5 Replies)
Discussion started by: harpreetanand
5 Replies

3. Shell Programming and Scripting

Searching for a file, with name Last Sunday through Saturday

Hi List, I need write a script that looks for a file with name File_03-09_to_03-15_data.txt (File_LastSunday_to_LastSaturday_data.txt). If this file is not exists i have to look for a pervious weeks file like File_03-02_to_03-08_data.txt. This loop should continue untill it get a file or no file... (0 Replies)
Discussion started by: rejirajraghav
0 Replies

4. UNIX for Advanced & Expert Users

How to scedule a script to be run on every second saturday in Month?

Hello Friends, In my project I have to schedule a script to be run on every second saturday (once in month). Can you please suggest what entry should I make in cron ? (5 Replies)
Discussion started by: sourabhsharma
5 Replies

5. Windows & DOS: Issues & Discussions

How to get next Saturday's 'Date'?

Hi am trying to get the upcoming Saturday date in the batch file. Kindly help ASAP. its urgent. :confused: (10 Replies)
Discussion started by: Zensar
10 Replies

6. Shell Programming and Scripting

Run cron on every second Saturday ??

Hi, Can anyone help in editing CRON (OR) write a script to run another script every second saturday?? I tried to make use of DATE command to find the day but couldnt proceed further. your help is highly appreciated! Thanks, Mahi (11 Replies)
Discussion started by: mahi_mayu069
11 Replies

7. Shell Programming and Scripting

cron to get executed on 2nd and 4th saturday of every month

Hi , i need to reboot a server during 2nd and 4th saturday every month. i have come up with the below cron 30 17 8-14 * * if ; then /rebootscript; fi # to reboot every second saturday 30 17 22-28 * * if ; then /rebootscript; fi # to reboot every fourth saturday I am wondering why it... (3 Replies)
Discussion started by: chidori
3 Replies

8. Shell Programming and Scripting

Scheduling on every 2nd Saturday of a Month

Hi ! I need ur help on a UNIX scheduling Concept understanding : If I need to schedule a job (Say ETL Datastage job) through a shell script using the Cron function to make it run on every second saturday of every month, How can I do it ? :confused: (2 Replies)
Discussion started by: Ravichander
2 Replies

9. Shell Programming and Scripting

How to check day name is it saturday in bash shell script?

How to check the day name,is it saturday in bash shell script. If dayname = saturday then run the full load else run just the incremental loads end if Thank you very much for the helpful information. (4 Replies)
Discussion started by: cplusplus1
4 Replies
DATETIME.MODIFY(3)							 1							DATETIME.MODIFY(3)

DateTime::modify - Alters the timestamp

       Object oriented style

SYNOPSIS
public DateTime DateTime::modify (string $modify) DESCRIPTION
Procedural style DateTime date_modify (DateTime $object, string $modify) Alter the timestamp of a DateTime object by incrementing or decrementing in a format accepted by strtotime(3). PARAMETERS
o $object -Procedural style only: A DateTime object returned by date_create(3). The function modifies this object. o $modify -A date/time string. Valid formats are explained in Date and Time Formats. RETURN VALUES
Returns the DateTime object for method chaining or FALSE on failure. CHANGELOG
+--------+---------------------------------------------------+ |Version | | | | | | | Description | | | | +--------+---------------------------------------------------+ | 5.3.6 | | | | | | | Absolute date/time statements now take effect. | | | Previously, only relative parts were used. | | | | | 5.3.0 | | | | | | | Changed the return value on success from NULL to | | | DateTime. | | | | +--------+---------------------------------------------------+ EXAMPLES
Example #1 DateTime.modify(3) example Object oriented style <?php $date = new DateTime('2006-12-12'); $date->modify('+1 day'); echo $date->format('Y-m-d'); ?> Procedural style <?php $date = date_create('2006-12-12'); date_modify($date, '+1 day'); echo date_format($date, 'Y-m-d'); ?> The above examples will output: 2006-12-13 Example #2 Beware when adding or subtracting months <?php $date = new DateTime('2000-12-31'); $date->modify('+1 month'); echo $date->format('Y-m-d') . " "; $date->modify('+1 month'); echo $date->format('Y-m-d') . " "; ?> The above example will output: 2001-01-31 2001-03-03 SEE ALSO
strtotime(3), DateTime.add(3), DateTime.sub(3), DateTime.setDate(3), DateTime.setISODate(3), DateTime.setTime(3), DateTime.setTimes- tamp(3). PHP Documentation Group DATETIME.MODIFY(3)
All times are GMT -4. The time now is 02:24 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy