The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to scedule a script to be run on every second saturday in Month? sourabhsharma UNIX for Advanced & Expert Users 5 05-09-2008 04:32 AM
Searching for a file, with name Last Sunday through Saturday rejirajraghav Shell Programming and Scripting 0 03-19-2008 12:55 AM
How to check for Saturday or Sunday harpreetanand SUN Solaris 5 08-11-2007 01:31 PM
Date - Last Sunday thru Saturday skymirror Shell Programming and Scripting 2 12-12-2005 10:18 AM
Script to check for a file, check for 2hrs. then quit mmarsh UNIX for Dummies Questions & Answers 2 09-16-2005 11:46 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 12-20-2006
Registered User
 

Join Date: Apr 2006
Location: Mumbai
Posts: 13
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
Reply With Quote
Forum Sponsor
  #2  
Old 12-20-2006
Registered User
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,397
Code:
if [ $(date +%d) -eq $(cal | awk ' NF == 7 { day=$7 } END { print day } ') ]
then
   echo "Today is last Saturday"
fi
Reply With Quote
  #3  
Old 12-20-2006
justsam's Avatar
Registered User
 

Join Date: Oct 2006
Location: Bangalore,India
Posts: 37
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
Reply With Quote
  #4  
Old 12-20-2006
Registered User
 

Join Date: Oct 2005
Posts: 67
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.
Reply With Quote
  #5  
Old 12-20-2006
Registered User
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,397
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
Reply With Quote
  #6  
Old 12-20-2006
Registered User
 

Join Date: Aug 2006
Posts: 122
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 03:59 AM.
Reply With Quote
  #7  
Old 12-21-2006
Registered User
 

Join Date: Oct 2005
Posts: 67
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.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 12:15 PM.


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