1745788 Members
4035 Online
108722 Solutions
New Discussion юеВ

Report script

 
SOLVED
Go to solution
wayne_104
Regular Advisor

Report script

Hi

New to the scripting thing.

I have been asked to setup a report script. Calling report is not an issue. but they want diffrent things for diffrent days.

the requirement is this.

Check for first sunday of the year.
check for first sunday in june.
check for first sunday of the month
check for sunday of week.


I was thinking of using a case statment. for this.

But the problem is i am not sure of how to check for the date in each of these instances.


I was thinking of using this command.
date "+ %U:%m/%d:%C%y:%A:%M:%S"

then grepping the information i needed.

Am i on the right track.
17 REPLIES 17
Matti_Kurkela
Honored Contributor

Re: Report script

You're sort of on the right track.

You don't have to use grep. Just assign the individual date elements to variables, and then you can use them in case or if/then/else comparisions as much as you wish.

For example:

MONTHNR=$(date +%m) # two digits: 00...12
DAYOFMONTH=$(date +%d) # 00...31
DAYOFWEEK=$(date +%w) # Sunday = 0, Saturday = 6

If you're running the report scripts from crontab, note that it can run things on a particular day of the week.

> check for sunday of week.

Just set a cron job to run on Sundays only.
For example, to run a script at 04:00 on Sundays, the cron job entry would be:

00 04 * * 0 /somewhere/script.sh

Or using the variables defined above:
if [ "$DAYOFWEEK" = 0 ]; then
# create report
fi

> Check for first sunday of the year.

If it's Sunday, and the month is January, and the day-of-month number is between 1 and 7 (inclusive), it must be the first Sunday of the year, right?

Using the variables defined above:

if [ "$DAYOFWEEK" = 0 -a "$MONTHNR" = "01" -a "$DAYOFMONTH" -ge 1 -a "$DAYOFMONTH" -le 7 ]; then
# create report
fi

You can get the first Sunday of June from the above by just adjusting the month number.

First Sunday of any month: just leave out the test for the month number.

if [ "$DAYOFWEEK" = 0 -a "$DAYOFMONTH" -ge 1 -a "$DAYOFMONTH" -le 7 ]; then
# create report
fi

MK
MK
wayne_104
Regular Advisor

Re: Report script

Hi That looks great but a bit complicated for me to understand.

I was looking at something like this.


DAT=`date "+ %m:%w:%C%y:%A"`
echo $DAT
case $DAT in
00:00:2010:Sunday| echo test1;;
06:00:2010:Sunday| echo test2;;
01|02|03|04|05|07|08|09|10|11|12:00:2010:Sunday| echo test3;;
esac
wayne_104
Regular Advisor

Re: Report script

Hi That looks great but a bit complicated for me to understand.

I was looking at something like this.


DAT=`date "+ %m:%w:%C%y:%A"`
DAT_month=`date "+ %m"`
DAT_day=`date "+ %w"`
DAT_year=`date "+ %C%y"`
DAT_day_name=`date "+ %A"`
echo $DAT
case $DAT in
01:00:$DAT_year:Sunday) echo "test1";;
06:00:$DAT_year:Sunday) echo "test2";;
01|02|03|04|05|07|08|09|10|11|12:00:$DAT_year:Sunday) echo "test3";;
02|03|04|05|07|08|09|10|11|12:00:$DAT_year:Sunday) echo "test4";;
10:4:2010:Thursday) echo "test5";;
esac
wayne_104
Regular Advisor

Re: Report script

why will my echo test4 not work.

my echo test5 does.

DAT=`date "+%m:%w:%C%y:%A"`
DAT_month=`date "+%m"`
DAT_day=`date "+%w"`
DAT_year=`date "+%C%y"`
DAT_day_name=`date "+%A"`
echo $DAT
case $DAT in
01:00:$DAT_year:Sunday) echo "test1";; #first sunday of the year
06:00:$DAT_year:Sunday) echo "test2";; #First sunday of June
02|03|04|05|07|08|09|10|11|12:00:$DAT_year:Sunday) echo "test3";; #First Sunday of Month
02|03|04|05|07|08|09|10|11|12:00:$DAT_year:Sunday) echo "test4";; #all other sunday
$DAT_month:1|2|3|4|5|6:$DAT_year:Monday|Teusday|Wednesday|Thursday|Friday|Saturday) echo "test4";; #all other d
ays
$DAT_month:$DAT_day:$DAT_year:$DAT_day_name) echo "test5";;
esac
Dennis Handly
Acclaimed Contributor

Re: Report script

>but a bit complicated for me to understand.

Why do you think pattern matching or regular expressions or even EREs are easier to understand than procedural programming, like in C or even COBOL?

>DAT_year=`date "+%C%y"`

Why not use just the Y2K format "+%Y"?

You do know that asking for the time in N separate calls is gong to get you into trouble around midnight?

>02|03|04|05|07|08|09|10|11|12:00:$DAT_year:Sunday) echo "test3";; #First Sunday

This is going to match $DAT with the strings:
02, 03, ... 11 or 12:10:$DAT_years:Sunday.

If you were using EREs, you could use () to enclose the various months.
wayne_104
Regular Advisor

Re: Report script

hi as i said from begining not at all sure about any of this.

First time doing this stuff. I am just reading and putting this together as i go and testing to see if it works.
Jim Walls
Trusted Contributor

Re: Report script

The cron knows about days of the week, month etc. And so do the "at" or "batch" commands.

I use this crontab entry to run a job on the first Sunday of the month:

0 0 1 * * at -f myscript 03:45 sun


This will run myscript at 3:45am on the first Sunday of every month.

In your case, myscript only has to work out what it needs to run.
Billa-User
Regular Advisor

Re: Report script

hello,

i had also this issue, i am using following
scripts (found in the net):

script to search:
weekselector.sh or weekselector

requirement:
datecalc.sh or datecalc

regards,
Solution

Re: Report script

Hello,

Here is my proposal based on the fact that when you execute the command "cal ", the first Sunday of a month is always the last column of the third line

#!/usr/bin/ksh

function get_first_sunday
{
typeset month=$1 year=$2

typeset -Z02 first_sunday=$( cal $month $year | awk 'NR == 3 { print $NF }' )

print "Sun" $year $month $first_sunday
}

typeset wday month year
typeset -Z02 mday

date +'%a %m %e %Y' | read wday month mday year

typeset first_sunday_of_the_year=$( get_first_sunday 01 $year )
typeset first_sunday_of_june=$( get_first_sunday 06 $year )
typeset first_sunday_of_the_month=$( get_first_sunday $month $year )

case "$wday $year $month $mday" in

"$first_sunday_of_the_year")
print "First sunday of the year"
;;
"$first_sunday_of_june")
print "First sunday of June"
;;
"$first_sunday_of_the_month")
print "First sunday of the month"
;;
Sun*)
print "Sunday"
;;
esac

Cheers,

Jean-Philippe