ms:/home/unxsa/functions>today
27May2011
ms:/home/unxsa/functions>today -p 30
26Jun2011
ms:/home/unxsa/functions>cat today
# Name: today
#
# Purpose: Prints today's date, plus or minus the given number of days.
# Default is today's date.
#
# Date: 2006/06/08
#
# Version: 1.0
#
# Required Parameters:
# -------------------------------------------------------------------------
#
# Optional Parameters:
# -f [format]- format of date to use
# -p #- number of days to add
# -m #- number of days to subtract
# -c c- character to use in the date string
# -s- use short year string
# -------------------------------------------------------------------------
#
# Change History:
#
# Date Name Comments
# _________________________________________________________________________
# 2006/06/08 selbyp Created.
# 21 jan 2011 purdym converted to ksh93
#
#
#
#
#
#
#
#
#
#
#
#
###################################################################################
# function
###################################################################################
function today {
USAGE=":f:p:m:c:sh"
FORMAT=0
PLUS=0
MINUS=0
SEPCHAR=""
SHORT=0
if [[ $# -gt 0 ]]
then
while getopts "${USAGE}" OPT
do
case ${OPT} in
f) FORMAT=${OPTARG};;
p) PLUS=${OPTARG};;
m) MINUS=${OPTARG};;
c) SEPCHAR=${OPTARG};;
s) SHORT=1;;
h) cat <<EOF
Syntax: today { [-p n] OR [-m n] } [-f FORMAT] [-c CHAR] [-s] [-h]
Parameters:
[-p n]-Print today's date, plus n days
[-m n]-Print today's date, minus n days
[-f FMT]-Print date using specified format (DMY/MDY/YMD), default looks like Jun092006
[-c c]-Separate dates using the character c
[-s]-Use a short year in the date
[-h]-View this help
Default: Print today's date in the default format.
EOF
return 0
;;
?) echo "today(): unrecognized option: ${OPT}"
return 1;;
esac
done
shift $((${OPTIND} - 1))
ARGUMENTS=$@
if (( ${PLUS} != 0 && ${MINUS} != 0 ))
then
RETCODE=1
(( ${DEBUG} != 0 )) && print "today(): Attempted to add and subtract simultaneously."
return $RETCODE
fi
if [[ ${FORMAT} -ne 0 && ${FORMAT} != "YMD" && ${FORMAT} != "MDY" && ${FORMAT} != "DMY" ]]
then
RETCODE=2
(( ${DEBUG} != 0 )) && print "today(): Unrecognized format: ${FORMAT}"
return $RETCODE
fi
if (( ${#SEPCHAR} > 1 ))
then
RETCODE=3
(( ${DEBUG} -ne 0 )) && print "today(): Bad separating character: ${SEPCHAR}"
return $RETCODE
fi
fi
eval `date "+DAY=%d; MONTH=%m; YEAR=%Y`
if (( $PLUS > 0 ))
then
DAY=$((DAY + PLUS))
elif (( $MINUS > 0 ))
then
DAY=$((DAY - MINUS))
fi
if (( $DAY <= 0 ))
then
while (( $DAY <= 0 ))
do
MONTH=$((MONTH - 1))
if (( $MONTH == 0 ))
then
YEAR=$(( YEAR - 1))
MONTH=12
fi
set -A DAYS $(cal $MONTH $YEAR)
XDAY=${DAYS[$((${#DAYS[*]} - 1 ))]}
DAY=$((XDAY + DAY))
done
else
set -A DAYS $(cal $MONTH $YEAR)
MAXDAY=${DAYS[$((${#DAYS[*]} - 1 ))]}
if (( $DAY > $MAXDAY ))
then
while (( $DAY > $MAXDAY ))
do
DAY=$((DAY - MAXDAY))
MONTH=$((MONTH + 1))
if (( MONTH > 12 ))
then
YEAR=$((YEAR + 1))
MONTH=1
fi
set -A DAYS $(cal $MONTH $YEAR)
MAXDAY=${DAYS[$((${#DAYS[*]} - 1 ))]}
done
fi
fi
if (( $SHORT != 0 ))
then
if (( ${#YEAR} == 4 ))
then
OLD_YEAR=${YEAR}
typeset -Z2 YEAR
YEAR=$( echo ${OLD_YEAR} | cut -c 3-4 )
elif (( ${#YEAR} != 2 ))
then
if [[ ! -z $DEBUG ]] && [[ $DEBUG -gt 0 ]]
then
echo "today(): what kind of year is this: ${YEAR}??"
return 1
fi
fi
fi
typeset -Z2 F_DAY F_MONTH
F_DAY=$DAY
F_MONTH=$MONTH
if [[ $FORMAT == "YMD" ]]
then
print "${YEAR}${SEPCHAR}${F_MONTH}${SEPCHAR}${F_DAY}"
elif [[ $FORMAT == "DMY" ]]
then
print "${F_DAY}${SEPCHAR}${F_MONTH}${SEPCHAR}${YEAR}"
elif [[ $FORMAT == "MDY" ]]
then
print "${F_MONTH}${SEPCHAR}${F_DAY}${SEPCHAR}${YEAR}"
else
case $F_MONTH in
01) MONTH_STR=Jan;;
02) MONTH_STR=Feb;;
03) MONTH_STR=Mar;;
04) MONTH_STR=Apr;;
05) MONTH_STR=May;;
06) MONTH_STR=Jun;;
07) MONTH_STR=Jul;;
08) MONTH_STR=Aug;;
09) MONTH_STR=Sep;;
10) MONTH_STR=Oct;;
11) MONTH_STR=Nov;;
12) MONTH_STR=Dec;;
esac
print ${F_DAY}${MONTH_STR}${YEAR}
fi
}
Hi,
I would like to find out how can i calculate a date which is 3 months ago. I intend to run a cron job on the 1st of every month, and calculate the month 4 months earlier from the date. For example, if today's date is 1st May 2007, i would like to return 012007( January 2007).
i can get... (1 Reply)
hi,
I have a big file that contains datas since 4 years ago.
I need re-create this file but just lines that are 13 months ago from today.
see what I have: ( I have a file.ksh that calls this file.scl ok !!)
======== file.scl ================
/STATISTICS=stderr
/STABLE
/NODUPLICATES... (3 Replies)
hi,
I have a big file that contains datas since 4 years ago.
I need re-create this file but just lines that are 13 months ago from today.
see what I have: ( I have a file.ksh that calls this file.scl ok !!)
======== file.scl ================
/STATISTICS=stderr
/STABLE
/NODUPLICATES... (4 Replies)
I am new to shell scripting and doing a similar thing @ work
Stan:Smith:Detroit:MI
Jim:Jones:Farmington Hills:MI
Jack:Frost:Denver:CO
Sue:Apple:New York:NY
Cindy:Thompson:Battle Creek:MI
John:Smith:Denver:CO
George:Jones:New York:NY
Need to create a shell script This script will display... (1 Reply)
Hi,
I'm curious to know if we can display 5 months of a calendar using the cal command. I know we can three successive months (cal -3) but I wanted to know if we can do it with 5 months for example. (Give a specific month, and get as a result two previous months + the month in question + two... (12 Replies)
Please find my below requirement and see if you can help me on this. I am looking for a shell script which can provide me the below output.
Manuall steps which i am doing now
1) First I source the File
$ . ./WC_env.sh
2) Execute the command
$ /app/oracle/product/mos/bin/mosotl -url... (2 Replies)
hi all i have a reporting work and i want it to be automated using shell scripting kindly let me know how can i make that possibe .
eg data are :... (2 Replies)
topfunc()
{
top
}
topfunc
Here i used the top command inside a function,and i called the function.
when executing this bash file i get all the process which are using by the kernel
i just want to display only the first 5 running process.
is it possible? (7 Replies)