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
FILE_PATH=/export/home/jaqst/training/testdir_1, which is stored into your variableFILE_PATHand there is no file of this name (as you can verify by doing als FILE_PATH=/export/home/jaqst/training/testdir_1). You have to write a proper parser for your configuration file.if ...; then; fi; if [[ $? -ne 0 ]]; then ...; fiis pretty convoluted. Relying on the return value of a command inside the if clause is not particularly maintainable. It might be better to write:if [ "$age" -gt "$DAYS" ]; then if ! rm "$FILE"; then ...; fi; fiBut note that there is really no need for you to be writing your own error messages.rmis going to print an error message, which will then be supplemented (?) by your own "failed to remove file". (Your error messages, however, are going to the wrong stream. You should useecho >&2 ...)