Report script (613 Views)
Reply
Regular Advisor
wayne_104
Posts: 144
Registered: ‎01-26-2005
Message 1 of 18 (613 Views)
Accepted Solution

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.
Honored Contributor
Matti_Kurkela
Posts: 6,271
Registered: ‎12-02-2001
Message 2 of 18 (613 Views)

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
Regular Advisor
wayne_104
Posts: 144
Registered: ‎01-26-2005
Message 3 of 18 (613 Views)

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
Regular Advisor
wayne_104
Posts: 144
Registered: ‎01-26-2005
Message 4 of 18 (613 Views)

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
Regular Advisor
wayne_104
Posts: 144
Registered: ‎01-26-2005
Message 5 of 18 (613 Views)

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
Acclaimed Contributor
Dennis Handly
Posts: 25,291
Registered: ‎03-06-2006
Message 6 of 18 (613 Views)

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.
Regular Advisor
wayne_104
Posts: 144
Registered: ‎01-26-2005
Message 7 of 18 (613 Views)

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.
Trusted Contributor
Jim Walls
Posts: 140
Registered: ‎05-05-2001
Message 8 of 18 (613 Views)

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.
Regular Advisor
Billa-User
Posts: 159
Registered: ‎02-16-2004
Message 9 of 18 (613 Views)

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,
Advisor
Jean-Philippe Henry
Posts: 18
Registered: ‎06-30-2002
Message 10 of 18 (613 Views)

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
Regular Advisor
wayne_104
Posts: 144
Registered: ‎01-26-2005
Message 11 of 18 (613 Views)

Re: Report script

Okay The customer wants to now do backups fro the rest of the week days.

I made the following modification but it does not work.

I donot see the file in /tmp

what have i done wrong?

#!/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
}
function get_other_day
{
typeset month=$1 year=$2

typeset -Z02 other_day=$( cal $month $year )

print "other" $year $month $other_day
}

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 )
typeset other_day=$( get_other_day $month $year )
echo "$other_day"
echo $first_sunday_of_the_year
echo $first_sunday_of_june
echo $first_sunday_of_the_month


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

"$first_sunday_of_the_year")
echo "First sunday of the year" > /tmp/First_year.txt
;;
"$first_sunday_of_june")
echo "First sunday of June" > /tmp/First_june.txt
;;
"$first_sunday_of_the_month")
echo "First sunday of the month" > /tmp/First_month.txt
;;
"$other_day")
echo "Other" > /tmp/otherday.txt
;;
Sun*)
echo "Other" > /tmp/sunday.txt
;;

esac
Acclaimed Contributor
Dennis Handly
Posts: 25,291
Registered: ‎03-06-2006
Message 12 of 18 (613 Views)

Re: Report script

Add the following to the end of your case:
*) echo "no match $other_day" ;;

There may be a spacing problem in your get_other_day function? Also how does the string "other" ever match $wday?
Regular Advisor
wayne_104
Posts: 144
Registered: ‎01-26-2005
Message 13 of 18 (613 Views)

Re: Report script

I cant answer your question because i do not understand The wday thing.

I am new to this and just when i thought i understood i found i did not.
Acclaimed Contributor
Dennis Handly
Posts: 25,291
Registered: ‎03-06-2006
Message 14 of 18 (613 Views)

Re: Report script

>I do not understand the wday thing.

To get a match in the case, these strings must match:
echo "$wday $year $month $mday"
echo "$other_day"
Regular Advisor
wayne_104
Posts: 144
Registered: ‎01-26-2005
Message 15 of 18 (613 Views)

Re: Report script

ok i get it now thank you for all your help.

Regular Advisor
wayne_104
Posts: 144
Registered: ‎01-26-2005
Message 16 of 18 (613 Views)

Re: Report script

Man i have a long way to go with this stuff

Thanks for your help
Acclaimed Contributor
Dennis Handly
Posts: 25,291
Registered: ‎03-06-2006
Message 17 of 18 (613 Views)

Re: Report script

>I get it now thank you for all your help.

You can of course use a wildcard at the end of your case that matches all other cases:
*)
echo "Other" > /tmp/otherday.txt ;;

And remove your "$other_day" case.
Regular Advisor
wayne_104
Posts: 144
Registered: ‎01-26-2005
Message 18 of 18 (613 Views)

Re: Report script

That is exactly what I did.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.