Please help me to write the following script.
1) Input files will be in Test directory. (Test/YYYY/MM) folders (Ex: 1) 2010/ 01 2) 2011/05)
The file name will be Testfile1_6676543218_Axxxxxxyyyyyzzzzzzzzzzzzzzzzzz.pdf.gz
2) The compare file will be in /tmp directory. (/tmp/comparefile.txt). The file will have list of 6 digit numbers. Ex:
(001234
015678
045679)
3) The batch has to compare 6 digit numbers in the compare file and 6 digit numbers in PDF file (xxxxxx - position 23 - 28) in the Test directory folders(Current month - Previous months). If it matches, then copy the pdf file to the new folder.
1) Conditions: It has to look for (Current month - Previous months) folder. Previous month should read from the property file. It can be either (01 ....12 months)
Ex: if Current month - 01 2011 and Previous month - 06 2010 then batch has to look since Aug 2010 to Jan 2011 ) folder
The compare file will have around 200 store numbers.
Ex: (001345
013467
027890
011456)
The pdf files will be under Test/2010/01 -->
Testfile1_6676543218_A0013457899000232323232323232323232.pdf.gz
Testfile1_6676543218_A0013447899000232323232323232323232.pdf.gz
Testfile1_6676543218_A0134677899000232323232323232323232.pdf.gz
if current month is 01 and previous month is 06 ..then batch has to look for all the folders since Aug(2010/05 folder to 2011/01 folder).
Hope it clears. if 001345 matches(position 23 - 28) any of the above folder , then it should copy to the new folder(sample folder)
#! /bin/ksh
NAME=$(basename ${0})
FILE=$(mktemp --tmpdir=/tmp ${NAME}XXXXXX)
trap "rm -f ${FILE}.\*" EXIT
#-------------------------------------------------------------------------------
comparefile=
monthprior=
movetodir=
verbose=
while getopts :d:f:p:v OPT; do
case "${OPT}" in
d) if [[ ! -d "${OPTARG}" ]]; then
print -u2 ${NAME}: not a directory: "${OPTARG}"
exit 1
fi
if [[ ! -w "${OPTARG}" || ! -x "${OPTARG}" ]]; then
print -u2 ${NAME}: permission denied: "${OPTARG}"
exit 1
fi
movetodir="${OPTARG}"
;;
f) if [[ "${OPTARG}" != - && ! -e "${OPTARG}" ]]; then
print -u2 -- ${NAME}: no such file or directory - "${OPTARG}"
exit 1
fi
comparefile="${OPTARG}"
;;
p) if [[ "${OPTARG}" != [1-9][0-9]* ]]; then
print -u2 ${NAME}: invalid number of months: "${OPTARG}"
exit 1
fi
monthsprior=${OPTARG}
;;
v) verbose=verbose ;;
*) print -u2 -- "USAGE ${NAME} [-p <monthsprior>] [-c <comparefile] [-v] dir..."
exit 1
;;
esac
done
shift $(expr ${OPTIND} - 1)
if [[ -z "${movetodir}" ]]; then
print -u2 -- ${NAME}: missing destination directory
exit 1
fi
#-------------------------------------------------------------------------------
# build list of YYYY/MMs to process
current=$(date +%Y/%m)
typeset -Z4 Y=${current%/*}
typeset -Z2 M=${current#*/}
while [[ 0 -lt ${monthsprior} ]]; do
(( monthsprior = monthsprior - 1 ))
(( M = M - 1 ))
if [[ ${M} -lt 1 ]]; then
M=12
(( Y = Y - 1 ))
fi
print -- ${Y}/${M}
done > ${FILE}.ym
#-------------------------------------------------------------------------------
# validate directories
for dir in "${@}"; do
if [[ ! -d "${dir}" ]]; then
print -u2 -- "${dir}:" no such directory
continue
fi
if [[ ! -r "${dir}" || ! -w "${dir}" || ! -x "${dir}" ]]; then
print -u2 -- "${dir}:" permission denied
continue
fi
print -- "${dir}"
done > ${FILE}.dirs
#-------------------------------------------------------------------------------
grep -v '^#' "${comparefile:--}" | while read key; do
if [[ -n "${verbose}" ]]; then
print -- '*' "${key}"
fi
for dir in $(< ${FILE}.dirs); do
if [[ -n "${verbose}" ]]; then
print -- '/' "${dir}"
fi
for ym in $(< ${FILE}.ym); do
if [[ ! -d "${dir}/${ym}" ]]; then
continue
fi
ls "${dir}/${ym}" \
| grep "_A${key}[0-9]*.pdf.gz" \
| while read ${file}; do
if [[ -n "${verbose}" ]]; then
print -- '+' "${ym}/${file}"
fi
mv -f "${dir}/${ym}/${file}" "${movetodir}"
done
done
done
done