Check if a day eg: saturday is the Last saturday of the month!


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Check if a day eg: saturday is the Last saturday of the month!
# 1  
Old 12-20-2006
Check if a day eg: saturday is the Last saturday of the month!

Hi,

I need to develop a script to check for the day. If the day is a Saturday then the script should check if the Saturday is the last Saturday of the month so that certain set of instruction can be executed is it is the last Saturday. I do not want to use the crontab as this needs to be part of a workflow ... Any tips..

Regds.,
Jobby
# 2  
Old 12-20-2006
Code:
if [ $(date +%d) -eq $(cal | awk ' NF == 7 { day=$7 } END { print day } ') ]
then
   echo "Today is last Saturday"
fi

# 3  
Old 12-20-2006
Hi Anbu...

Jus a quick question... See the below commands i executed on command prompt:

qsaskot on eeiatuc930> cal
December 2006
S M Tu W Th F S
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
qsaskot on eeiatuc930> cal | awk ' NF == 7 { day=$7 } { print day } '

S
S
9
16
23
30
30

If u see the above output, it displays some values (S,30) twice...
Any reason y it is so...

Rgds,
JustSam
justsam
# 4  
Old 12-21-2006
Explanation

I think i can explain this,

If u take the third line values 1 2 are in the 6th and 7th column position.But the awk assumes that there are only two fields in it and as it didnt has 7th column it prints the previous value 'S'.The same case for 30 also,If u take the last line there are only 1 field(31),again prints the previous value 30
And thats the reason it prints in this format.

Regards,
cskumar.
# 5  
Old 12-21-2006
Code:
S M Tu W Th F S
1 2

In the above input first line contains 7 fields and S is assigned to variable day.For the next line of input NF == 7 is not satisfied and value of day is not affected.
But
Code:
{ print day }

is executed for every line of input and prints the value in variable day
# 6  
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..
# 7  
Old 12-21-2006
I have tried the script.check it.

echo "******************************************************************************"
echo "*****Usage"
echo "*****Month[value like "01/02/03/04/05/06/07/08/09/10/11/12"]"
echo "*****DAY_OF_THE_WEEK [value like "Sun/Mon/Tue/Wed/Thu/Fri/Sat"]"
echo "*****FIRSTORLAST [value like "FIRST_DAY_OF_THE_WEEK/LAST_DAY_OF_THE_WEEK"]"
echo "******************************************************************************"

if [ $# -ne 3 ]
then
echo "Usage not proper: use like calendar.sh <<MONTH>> <<DAY_OF_THE_WEEK>> <<FIRST_OR_LAST>>"
echo "Today's Date"
SYSDATE=`date +%d`
echo "Date-->"$SYSDATE
MONTH=`date +%m`
echo "Month-->"$MONTH
YEAR=`date +%Y`
echo "Year-->"$YEAR
DAY_OF_THE_WEEK=`date +%a`
echo "Day of the week-->"$DAY_OF_THE_WEEK
FIRSTORLAST="FIRST_DAY_OF_THE_WEEK"
#echo "First or Last Day of the Week--->"$FIRSTORLAST
else
MONTH=$1
DAY_OF_THE_WEEK=$2
FIRSTORLAST=$3
#YEAR=$4
fi
case $DAY_OF_THE_WEEK in
Sun) startpos="0"
;;
esac
case $DAY_OF_THE_WEEK in
Mon) startpos="4"
;;
esac
case $DAY_OF_THE_WEEK in
Tue) startpos="7"
;;
esac
case $DAY_OF_THE_WEEK in
Wed) startpos="10"
;;
esac
case $DAY_OF_THE_WEEK in
Thu) startpos="13"
;;
esac
case $DAY_OF_THE_WEEK in
Fri) startpos="16"
;;
esac
case $DAY_OF_THE_WEEK in
Sat) startpos="19"
;;
esac

#echo "Starting position-->"$startpos

cal $MONTH 2006 | awk -v spos=$startpos ' NR > 2 { print substr($0,spos,2) }'|awk -v var=$FIRSTORLAST ' BEGIN { first="F"}
{
if (var=="FIRST_DAY_OF_THE_WEEK")
{
if (first=="F")
{
if ( $0 ~ /[0-9]/ )
{
day=$0
first="T"
}
else
{
first="F"
}
}
}
if (var=="LAST_DAY_OF_THE_WEEK")
{
if (length($0) != 0 )
day=$0}
}
END{
if (first=="T")
{
print var"-->"day
}
else
{
print var"-->"day
}
}'



You can even tune for the date calculation,

Regards,
cskumar.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

9. 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
Login or Register to Ask a Question