0

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
6
  • 2
    The first word in the first line of your config file seems to be the string FILE_PATH=/export/home/jaqst/training/testdir_1, which is stored into your variable FILE_PATH and there is no file of this name (as you can verify by doing a ls FILE_PATH=/export/home/jaqst/training/testdir_1). You have to write a proper parser for your configuration file. Commented Jul 12, 2023 at 11:31
  • 1
    if ...; then; fi; if [[ $? -ne 0 ]]; then ...; fi is 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; fi But note that there is really no need for you to be writing your own error messages. rm is 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 use echo >&2 ...) Commented Jul 12, 2023 at 12:32
  • @user1934428 You pushed me in the right direction with this, thank you. I've made some alterations and now have it working. Commented Jul 12, 2023 at 14:21
  • 2
    If you need no further assistance, please either provide an answer or remove the question. Commented Jul 12, 2023 at 15:52
  • @STAKD : Note that it is allowed to answer your own question. If you think that the solution you found is a useful contribution, feel free to post it as an answer. Commented Jul 13, 2023 at 5:47

1 Answer 1

0

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}
Sign up to request clarification or add additional context in comments.

1 Comment

Instead of while read line; do FILE_PATH=$(cut ..., you can let read split the line for you with while IFS=, read _ FILE_PATH _ DAYS; do ... the underscores are used to discard the unused columns. (eg, you could also write while IFS=, read a FILE_PATH b DAYS; do ..., or whatever names you want to use to store the unused columns)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.