#!/bin/ksh
######### Environment Setup #########
PATH=/gers/nurev/menu/pub/sbin:/gers/nurev/menu/pub/bin:/gers/nurev/menu/pub/mac
:/gers/nurev/menu/adm/sbin:/gers/nurev/menu/adm/bin:/gers/nurev/menu/adm/mac:/ge
rs/nurev/custom:/gers/nurev/fix:/gers/nurev/src_rev/fix:/gers/nurev/opt/path:/ge
rs/nurev/bin:/g/bin:/usr/bin:/etc:/usr/sbin:/usr/ucb:/sbin:.
ORACLE_HOME=/gers/nurev
ORACLE_SID=nurev
export PATH
export ORACLE_HOME
export ORACLE_SID
########## Global Variables ##########
DATE=$(date +%m%d%y)
TIME=$(date +%H%M)
DatafilesDir="/gers/nurev/datafiles"
PrintDir="/gers/nurev/print"
TempDir="/gers/nurev/tmp"
Logfile="/tmp/test_str_process_$DATE.log"
arrived=1
########## Function to Verify File is Completely Uploaded ###########
# Function : is_file_arrived file
# Arg(s) : file = file to verify
# Output : None
# Status : 0 = yes file arrived, 1 = no
# Env. : IFA_WAIT : interval (secs) for file size check (def=5)
#
is_file_arrived() {
[ -z "$1" ] && return 1
local file=$1
arrived=1
local size1 size2
if [ -f "$file" -a -z "$(fuser $file 2> /dev/null)" ] ; then
size1=$(ls -l $file 2>/dev/null | awk '{print $5}')
sleep ${IFA_WAIT:-5}
size2=$(ls -l $file 2>/dev/null | awk '{print $5}')
[ ${size1:-1} -eq ${size2:-2} ] && arrived=0
else
log_it "Store file $file not done uploading at $TIME on $DATE"
fi
log_it "Arrived is $arrived"
return $arrived
}
######### GERS Processing of File ###########
processFile ()
{
local fileName=$1
local fileExtension=$2
local fileNewName="$DatafilesDir/str${fileExtension}.asc"
local fileODIName="str${fileExtension}.pos"
mv -Eignore $fileName $fileNewName
prepup $fileNewName $fileExtension
mv -Eignore $PrintDir/$fileODIName $TempDir/$fileODIName
save2tmp $fileExtension
call_siu $fileExtension
log_it "Store file $fileName processed at $TIME on $DATE"
}
########## Log File Function ###########
log_it()
{
printf "%s\n" "$*" >> "$Logfile"
}
########## Main Processing ###########
nsec=1
while [[ "$(date +%H%M)" -lt 2340 ]]
do
for fileName in $DatafilesDir/[Uu][Pp][Ll][Oo][Aa][Dd].[0-9][0-9][0-9][0-9]
do
fileExtension=${fileName#*.}
is_file_arrived "$fileName"
if [ $arrived = 0 ]; then
if [ ! -f "$TempDir/poll_$fileExtension.txt" ]; then
nsec=1
processFile $fileName $fileExtension
else
log_it "Store $fileExtension is already in process"
fi
fi
done
sleep $nsec
case $nsec in
1) nsec=15;;
15) nsec=45;;
45) nsec=90;;
90) nsec=300;;
300) nsec=600;;
*) nsec=900;;
esac
done
...the script works well, except for that it logs information even when there is no file in the "for" loop, so the log looks like:
Arrived is 0
Store file /gers/nurev/datafiles/upload.0031 processed at 0127 on 111606
Arrived is 0
Store file /gers/nurev/datafiles/upload.0032 processed at 0127 on 111606
Store file /gers/nurev/datafiles/[Uu][Pp][Ll][Oo][Aa][Dd].[0-9][0-9][0-9][0-9] n
ot done uploading at 0127 on 111606
Arrived is 1
Store file /gers/nurev/datafiles/[Uu][Pp][Ll][Oo][Aa][Dd].[0-9][0-9][0-9][0-9] n
ot done uploading at 0127 on 111606
Arrived is 1
I would like for it to only log activity when it finds and actual file to process?
When generating file list, ksh returns the pattern itself if no file match the pattern.
If the is no file matching the pattern $DatafilesDir/[Uu][Pp][Ll][Oo][Aa][Dd].[0-9][0-9][0-9][0-9]
the for loop is executed one time with the value /gers/nurev/datafiles/[Uu][Pp][Ll][Oo][Aa][Dd].[0-9][0-9][0-9][0-9] (as you can see in the logfile).
Replace the for in loop by :
ls $DatafilesDir/[Uu][Pp][Ll][Oo][Aa][Dd].[0-9][0-9][0-9][0-9] 2>/dev/null | \
while read filename
do
. . .
done
I replaced the "for in" loop but it didn't process the files correctly, the section now looks like:
nsec=1
while [[ "$(date +%H%M)" -lt 2340 ]]
do
ls $DatafilesDir/[Uu][Pp][Ll][Oo][Aa][Dd].[0-9][0-9][0-9][0-9] 2>/dev/null \
while read filename
do
fileExtension=${fileName#*.}
is_file_arrived "$fileName"
if [ $arrived = 0 ]; then
if [ ! -f "$TempDir/poll_$fileExtension.txt" ]; then
nsec=1
processFile $fileName $fileExtension
else
log_it "Store $fileExtension is already in process"
fi
fi
done
sleep $nsec
case $nsec in
1) nsec=15;;
15) nsec=45;;
45) nsec=90;;
90) nsec=300;;
300) nsec=600;;
*) nsec=900;;
esac
done
It didn't create a log file at all, but the process did:
I also tried to just stop the string "/gers/genret/datafiles/[Uu][Pp][Ll][Oo][Aa][Dd].[0-9][0-9][0-9][0-9]" from being passed to the "is_file_arrived" function and thus it wouldn't get logged:
nsec=1
while [[ "$(date +%H%M)" -lt 2340 ]]
do
for fileName in $DatafilesDir/[Uu][Pp][Ll][Oo][Aa][Dd].[0-9][0-9][0-9][0-9]
do
fileExtension=${fileName#*.}
if [ $fileName <> "/gers/genret/datafiles/[Uu][Pp][Ll][Oo][Aa][Dd].[0-9][0
-9][0-9][0-9]" ] ; then
is_file_arrived "$fileName"
if [ $arrived = 0 ]; then
if [ ! -f "$TempDir/poll_$fileExtension.txt" ]; then
nsec=1
processFile $fileName $fileExtension
else
log_it "Store $fileExtension is already in process"
fi
fi
fi
done
sleep $nsec
case $nsec in
1) nsec=15;;
15) nsec=45;;
45) nsec=90;;
90) nsec=300;;
300) nsec=600;;
*) nsec=900;;
esac
done
but it still generates entries in the log file? The look like:
Arrived is 0
Store file /gers/genret/datafiles/UPLOAD.0007 processed at 1218 on 111806
Arrived is 0
Store file /gers/genret/datafiles/UPLOAD.0012 processed at 1218 on 111806
Arrived is 0
Store file /gers/genret/datafiles/[Uu][Pp][Ll][Oo][Aa][Dd].[0-9][0-9][0-9][0-9]
processed at 1218 on 111806
You miss the pipeline character between ls and while statements
nsec=1
while [[ "$(date +%H%M)" -lt 2340 ]]
do
ls $DatafilesDir/[Uu][Pp][Ll][Oo][Aa][Dd].[0-9][0-9][0-9][0-9] 2>/dev/null | \
while read filename
do
fileExtension=${fileName#*.}
is_file_arrived "$fileName"
if [ $arrived = 0 ]; then
if [ ! -f "$TempDir/poll_$fileExtension.txt" ]; then
nsec=1
processFile $fileName $fileExtension
else
log_it "Store $fileExtension is already in process"
fi
fi
done
sleep $nsec
case $nsec in
1) nsec=15;;
15) nsec=45;;
45) nsec=90;;
90) nsec=300;;
300) nsec=600;;
*) nsec=900;;
esac
done