Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

unix script for first working day of the month

Posted on 2005-04-10
12
Medium Priority
?
1,191 Views
Last Modified: 2013-12-26
to determine the first working day of the month (non sunday). I need a unix script for this and then execute the file on first working day of the month.
0
Comment
Question by:mahender20
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 48

Accepted Solution

by:
Tintin earned 500 total points
ID: 13749995
What happens with holidays that fall on a Monday, do you want to cater for that?

If not, then you can do:

if [ `date +%d` -le 7 -a `date +%a` = Mon ]
then
     echo "First working day of the week"
fi





0
 
LVL 6

Assisted Solution

by:Talmash
Talmash earned 500 total points
ID: 13750687
i can't think of a way solving without detailed cases ;

if (($date != $holiday1) && ($date != $holiday2) ....)

you may implement it other way, but need to "manually" check all posible holidays.

tal
0
 
LVL 29

Assisted Solution

by:MikeOM_DBA
MikeOM_DBA earned 500 total points
ID: 13753570
Here is one I coded some years ago to get the number of business days foir current date (not very efficient but it works):

#!/bin/ksh
#
# chk_business_day.ksh
#
# Returns number of business days for current date if current date
# is a business day, otherwise returns zero.
#
# Check the result with #?.
#
# Requires XX_holidays.dat table as list of holidays,
# where XX=Location or country and format:
# YYYYMMDD Holiday reason1
# YYYYMMDD Holiday reason2
# etc...
#
# Customize code where marked as %Customize%
#
# ------------------------------------------------------------------------
#  5/16/02    MOrtizM     Initial coding
#  8/20/02    MOrtizM     Corrected the business-day calculation and
#                         changed to return the number of business days
# ------------------------------------------------------------------------
LOC=$1    # Location (country)
#
# ------------------------------------------------------------------------
LOC=${LOC:=US}
LOCDIR='%Customize%'      # Directory where holiday table is located.
LOCDIR=/tmp
CURDT=$(date +%Y%m%d)
today=$(date +%d)
YYYYMM=$(date +%Y%m)
DOW=$(date +%w)
if [ ! -f $LOCDIR/${LOC}_holidays.dat ]
then
  echo "# ${LOC}_holidays table as of `date`" > $LOCDIR/${LOC}_holidays.dat
fi
if [[ $DOW -eq 0 || $DOW -eq 6 ]]
then
  return 0
fi
if test  $(grep "^${CURDT}" $LOCDIR/${LOC}_holidays.dat|wc -l) -gt 0
then
  return 0
fi
bus_day=0
for dd in 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 \
          16 15 14 13 12 11 10 09 08 07 06 05 04 03 02 01
do
  if test $dd -le $today
  then
    if [[ ! $DOW -eq 0 && ! $DOW -eq 6 ]]
    then
      if test  $(grep "^${YYYYMM}$dd" $LOCDIR/${LOC}_holidays.dat|wc -l) -eq 0
      then
        ((bus_day += 1 ))
      fi
    fi
    ((DOW -= 1))
    [ $DOW -lt 0 ] && DOW=6
  fi
done
return $bus_day
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 51

Assisted Solution

by:ahoffmann
ahoffmann earned 500 total points
ID: 13753581
dooh, I hate to be pedantic ..
please define "working day" (well, it's not sunday, but could it be saturday?)
which calender are you using?

i.g. I doubt that you'll find a good shell solution, I'd recommend perl's date::Manip
0
 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 13754066

True ahoffman, the script is for saturday/sunday non working days -- You would need to customize for other type of calendar.

Besides the script is supplied as an example only and which could provide a quick solution.

0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 13754383
MikeOM_DBA, my comment was meant as a general one, not to your good suggestion ;-)
0
 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 13755572

It's ok, with more time I definitely would seek a better solution with perl.

Besides, it is a bit cumbersome to use because you would have to:

1) Include the "working days" script in the target script and exit if it is NOT the business day you want (if [ $? -ne 1 ]...etc)

2) Execute target script from day1 to day3 or 4 (or 5 to get an adequate delta)



0
 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 13755597

PS: With globalization, it is imperative to get a  correct definition of "business days" for each country you will be dealing with.
0
 
LVL 48

Expert Comment

by:Tintin
ID: 13758318
With Perl, there's the very nice Date::Business module to work this sort of stuff out.

http://search.cpan.org/~desiminer/Date-Business-1.2/Business.pm
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month13 days, 5 hours left to enroll

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question