displaydom_k93
#!/usr/bin/ksh93
################################################################
function usagemsg_displaydom_k93 {
print "
Program: displaydom_k93
This script examines a calendar for a specified month and year and
outputs a day of the month or a list of days of the month on which a
specified day of the week occurs. For example, this script will output
the dates for all of the Sundays in February 2006, or it can output the
date of the second Sunday in February 2006.
Usage: ${1##*/} [-?] [-v] [-V] [-d WeekdayNumber] [-m Month -y Year]
[-o Occurance] [-D Delimiter] [-F]
Where:
'-d' = Weekday number, i.e., 0=Sun, 1=Mon, 2=Tue,
3=Wed, 4=Thu, 5=Fri, 6=Sat
Default value is 0 representing Sunday.
'-m' = Two digit month number from 1 - 12,
1=January, 2=February, 3=March, etc.
Default value is current month.
'-y' = Four digit year number. If the year is specified
on the command line, the month must also be specified.
Default value is current year.
'-o' = Single digit number specifying occurance
of the weekday during the month, i.e., 1=first,
2=second, 3=third, 4=fourth, 5=fifth.
Default value is all occurances.
'-D' = Single character to use as field delimiter between
dates in output stream.
Default value is colon (:)
'-F' = Full date output in format YYYYMMDD.
Default format is DD.
'-v' = Verbose mode
'-V' = Very Verbose Mode
Example: displaydom_k93 -d 0 -m 2 -y 2006
Author: Dana French (dfrench@mtxia.com)
Copyright 2006 by Dana French
\"AutoContent\" enabled"
return 0
}
################################################################
function displaydom_k93
{
typeset TRUE="0"
typeset FALSE="1"
typeset VERBOSE="${FALSE}"
typeset VERYVERB="${FALSE}"
typeset DOW="0"
typeset MON=""
typeset YEAR=""
typeset ICNT="0"
typeset OCCUR=""
typeset FULL="${FALSE}"
typeset VERSION="1.0"
typeset DELIM=" "
typeset TMP=""
typeset Y=""
typeset M=""
DAYPOS=( 0 3 6 9 12 15 18 )
while getopts ":d#m#y#o#D:FvV" OPTION
do
case "${OPTION}" in
'd') DOW="${OPTARG}";;
'm') MON="${OPTARG}";;
'y') YEAR="${OPTARG}";;
'o') OCCUR="${OPTARG}";;
'F') M=$( date +"%m" )
Y=$( date +"%Y" )
FULL="${TRUE}";;
'D') DELIM="${OPTARG}";;
'v') VERBOSE="${TRUE}";;
'V') VERYVERB="${TRUE}";;
'?') usagemsg_displaydom_k93 "${0}" && return 1;;
':') usagemsg_displaydom_k93 "${0}" && return 1;;
'#') usagemsg_displaydom_k93 "${0}" && return 1;;
esac
done
shift $(( ${OPTIND} - 1 ))
(( VERBOSE == TRUE )) && print -u 2 -- "# displaydom Version.......: ${VERSION}"
trap "usagemsg_displaydom_k93 ${0}" EXIT
if [[ "_${DOW}" != "_" ]] && (( DOW < 0 )) || (( DOW > 6 ))
then
print -u 2 "# ERROR: Invalid day of the week specified, (valid values 0-6)"
return 2
fi
if [[ "_${MON}" != "_" ]] && (( MON < 1 )) || (( MON > 12 ))
then
print -u 2 "# ERROR: Invalid month number specified, (valid values 1-12)"
return 3
fi
if [[ "_${YEAR}" != "_" ]] && (( YEAR < 0 ))
then
print -u 2 "# ERROR: Invalid year number specified"
return 4
fi
if [[ "_${OCCUR}" != "_" ]] && (( OCCUR < 1 )) || (( OCCUR > 5 ))
then
print -u 2 "# ERROR: Invalid occurance number specified, (valid values 1-5)"
return 5
fi
if [[ "_${YEAR}" != "_" ]] && [[ "_${MON}" == "_" ]]
then
print -u 2 "# ERROR: Month must also be specified when specifying year."
return 6
fi
if [[ "_${MON}" != "_" ]] && [[ "_${YEAR}" == "_" ]]
then
print -u 2 "# ERROR: Year must also be specified when specifying month."
return 7
fi
trap "-" EXIT
(( VERYVERB == TRUE )) && set -x
################################################################
(( FULL == TRUE )) &&
typeset -Z2 M="${MON:-$( date +'%m' )}" &&
typeset -Z4 Y="${YEAR:-$( date +'%Y')}"
cal ${MON} ${YEAR} | while IFS='' read -r -- LINE
do
[[ "_${LINE}" == _*+([a-zA-Z])* ]] && continue
if [[ "_${OCCUR}" != "_" ]]
then
[[ "_${LINE:${DAYPOS[DOW]}:2}" == _+([ ]) ]] && continue
(( OCCUR != ++ICNT )) && continue
fi
typeset -Z2 VAL="${LINE:${DAYPOS[DOW]}:2}"
(( VERBOSE == TRUE )) && [[ "_${VAL# }" == _+([0-9]) ]] &&
print -u 2 -- "# Adding ${Y}${M}${VAL# } to list of dates"
[[ "_${VAL# }" == _+([0-9]) ]] && TMP="${TMP} ${Y}${M}${VAL# }"
done
set -- ${TMP}
IFS="${DELIM:0:1}"
[[ "_${*}" != "_" ]] && print "${*}"
return 0
}
################################################################
displaydom_k93 "${@}"
|