If loop not ending

Hi,

The first if loop in the script not ending and going to infinite loop.

#!/usr/bin/ksh
set -vx
lc=1
st_date=$(date "+%Y%m%d")
LOGFILE=/home/infa_shared/OM_ftp_transfer.log.$st_date
file="/home/infa_shared/OM_WF.log.$st_date"
while [[ ! -f "$file"  ]]; do
if [[ $lc -lt 15 ]]
then
  sleep 15
  let lc=lc+1
print "Waiting for flow.dat file  for $lc minutes " >> $LOGFILE
   if [ -f "$file" ]
then
        echo "$file found.Transfering flow.dat dummy file at $(date)" >> $LOGFILE
        cd /home/infa_shared/
        echo "File created" >> $LOGFILE
        touch flow.dat
        chmod 777 om_flow.dat
        echo "FTP Process Started" >> $LOGFILE
        cp /home/infa_shared/OM_WORKFLOW.netrc $HOME/.netrc
        chmod 700 $HOME/.netrc
        ftp zwg47nix3v.comm.com
        rm $HOME/.netrc
        echo "FTP Process Completed" >> $LOGFILE
        rm flow.dat
        echo "File flow.dat deleted" >> $LOGFILE
        exit
      fi
fi
done

if [[ $lc -eq 15 ]]
then
print "Waited for 15 min for om_workflow.dat file" >> $LOGFILE
exit
fi

if [ -f "$file" ]
then
        echo "$file found.Transfering flow.dat dummy file at $(date)" >> $LOGFILE
        cd /home/infa_shared/
        echo "File created" >> $LOGFILE
        touch flow.dat
        chmod 777 flow.dat
        echo "FTP Process Started" >> $LOGFILE
        cp /home/infa_shared/OM_WORKFLOW.netrc $HOME/.netrc
        chmod 700 $HOME/.netrc
         ftp zwg47nix3v.comm.com
        rm $HOME/.netrc
        echo "FTP Process Completed" >> $LOGFILE
        rm flow.dat
        echo "File flow.dat deleted" >> $LOGFILE
              exit
        fi

debugging log -

+ [[ ! -f /home/infa_shared/OM_WF.log.20130605 ]]
+ [[ 15 -lt 15 ]]
+ [[ ! -f /home/infa_shared/OM_WF.log.20130605 ]]
+ [[ 15 -lt 15 ]]
+ [[ ! -f /home/infa_shared/OM_WF.log.20130605 ]]
+ [[ 15 -lt 15 ]]
+ [[ ! -f /home/infa_shared/OM_WF.log.20130605 ]]
+ [[ 15 -lt 15 ]]
+ [[ ! -f /home/infa_shared/OM_WF.log.20130605 ]]
+ [[ 15 -lt 15 ]]

If I gave condition eq it going infinite loop from starting.If less than given it waiting for 14 min after 15 its going to infinite loop

Thanks in advance

You have problem with while. as while loop is not ending.
Please change conditions in while loop.

Hi Pamu,

Thanks for reply

The main aim of this code is to check for a file, if not exist it have to wait 15 min for that file, later on it needs to come out of this while loop and execute another if loop(2nd) then log a msg. I am not sure why while loop not ending.

I would advise to you to start use of functions in the script.

while [[ ! -f "$file"  ]]; do
if [[ $lc -lt 15 ]]
then
  sleep 15  # this should 60 as it is in seconds
  let lc=lc+1
print "Waiting for flow.dat file  for $lc minutes " >> $LOGFILE
   if [ -f "$file" ]
then
        echo "$file found.Transfering flow.dat dummy file at $(date)" >> $LOGFILE
        cd /home/infa_shared/
        echo "File created" >> $LOGFILE
        touch flow.dat
        chmod 777 om_flow.dat
        echo "FTP Process Started" >> $LOGFILE
        cp /home/infa_shared/OM_WORKFLOW.netrc $HOME/.netrc
        chmod 700 $HOME/.netrc
        ftp zwg47nix3v.comm.com
        rm $HOME/.netrc
        echo "FTP Process Completed" >> $LOGFILE
        rm flow.dat
        echo "File flow.dat deleted" >> $LOGFILE
        exit
      fi
else
# do your second check as 15 minutes are already over when it comes to else part.
fi
done

you can add else part for the above loop.

1 Like

I think you want

while [[ ! -f "$file"  ]] && [[ $lc -lt 15 ]]; do
  sleep 60
  let lc=lc+1
done
if [[ -f "$file ]]
then
  ...
fi