Issue
I'm new to Korn shell scripting but learning as I go. I'm trying to run a WHILE loop with a nested FOR loop to read a CFG file and remove certain files that are older than a set number of days. The script runs fine without errors but it doesn't seem to be picking up the commands I have inside the loop. Below ill include the script, CFG file example and the output of the script with set -x included. Everything I've tried so far either fails or does not pick up the command. TIA!
CFG FILE;
FILE_PATH=/export/home/jaqst/training/testdir_1/ *.txt DAYS=7
FILE_PATH=/export/home/jaqst/training/testdir_1/ *.txt DAYS=14
Script;
#!/bin/ksh
set -x
. ${HOME}/.profile
#set up variables
CFGFILE=/export/home/jaqst/training
DATE=$(date +%s)
#
# Main processing starts here
#
echo `date +%d\ %b\ %H:%M` `basename $0` ${USER} "Job started"
#
#
# CD CFG File location
#
cd ${CFGFILE}
if [[ $? -ne 0 ]]
then
echo `date +%d\ %b\ %H:%M` `basename $0` ${USER} "failed to cd into ${CFGFILE}"
exit 9
fi
#
# Remove files that are older than specified age
#
while read FILE_PATH;
do
read DAYS
for FILE in $FILE_PATH/*; do
for TIME in $DAYS/*; do
if [ -f $FILE ]; then
if [ "$(find $FILE -mtime +$DAYS -print)" ]; then
rm -f $FILE
echo "Deleted $FILE"
fi
fi
done
done < hkeep_hera_cfg
#
if [[ $? -ne 0 ]]
then
echo `date +%d\ %b\ %H:%M` `basename $0` ${USER} "failed to remove file"
exit 9
fi
#
echo `date +%d\ %b\ %H:%M` `basename $0` ${USER} "Job Completed"
#
exit 0
Output;
12 Jul 11:44 hkeep_hera_draft jaqst Job started
+ cd /export/home/jaqst/training
+ [[ 0 -ne 0 ]]
+ 0< hkeep_hera_cfg
+ read FILE_PATH
+ read DAYS
+ 0< hkeep_hera_cfg
+ [ -f FILE_PATH=/export/home/jaqst/training/testdir_1 ]
+ [ -f FILE_PATH=/export/home/jaqst/training/testdir_1 ]
+ [ -f DAYS='7/*' ]
+ [ -f DAYS='7/*' ]
+ [[ 0 -ne 0 ]]
+ read FILE_PATH
+ date '+%d %b %H:%M'
+ basename ./hkeep_hera_draft
+ echo 12 Jul 11:44 hkeep_hera_draft jaqst 'Job Completed'
12 Jul 11:44 hkeep_hera_draft jaqst Job Completed
+ exit 0
From the output it also looks like it is only reading the first line in the CFG file.
I have tried a mixture of WHILE and FOR loop combinations and started to look into using the AWK command to see if this would allow the script to read the file easier.
I have also tried this method below but it errored out with the message - [52] [: argument expected
while read FILE_PATH; do
read DAYS
for FILE in $FILE_PATH/*; do
for TIME in $DAYS/*; do
mod_time=$(stat -c %Y $FILE)
current_time=$(date +%s)
age=$(( (current_time - mod_time) / (24 * 60) ))
if [ $age -gt $DAYS ]
then
rm $FILE
fi
if [[ $? -ne 0 ]]
then
echo `date +%d\ %b\ %H:%M` `basename $0` ${USER} "failed to remove file"
exit 9
fi
done < hkeep_hera_cfg
Solution
This is what I eventually ended up using to over come the issue of the script not reading the CFG correctly.
CFG EXAMPLE
TESTDIR1,/u01/home/dir1/dir2/test1,.txt,7
TESTDIR2,/u01/home/dir1/dir2/test2,.csv,7
SCRIPT
while IFS=, read ID FILE_PATH EXT DAYS ;
do
if [[ -z "${ID}" || -z "${FILE_PATH}" || -z "${EXT}" || -z "${DAYS}" ]];
then
echo `date +%d\ %b\ %H:%M` `basename $0` ${USER} "Failed to set up, please check config file." >> ${ERRLOG}
mail_err
exit 9
fi
#
cd ${FILE_PATH}
if [[ $? -ne 0 ]]
then
echo `date +%d\ %b\ %H:%M` `basename $0` ${USER} "failed to cd into ${FILE_PATH}" >> ${ERRLOG}
mail_err
exit 9
fi
#
find . \( -name ${EXT} \) -mtime +${DAYS} -exec rm -r {} 2>/dev/null \;
if [[ $? -ne 0 ]]
then
echo `date +%d\ %b\ %H:%M` `basename $0` ${USER} "failed to remove ${EXT} from ${FILE_PATH}" >> ${ERRLOG}
mail_err
exit 9
fi
done < ${CFGFILE}
Answered By - STAKD Answer Checked By - Mildred Charles (WPSolving Admin)