infinite while do loop problem

hi all,

this is how my scrip looks like

#!/bin/sh

bindir='/opt/apps/script/bin'
datadir='/opt/apps/script/data'
dir='/opt/apps/script'

while : ; do
     ls -1rt /opt/apps/script/data/check.txt*|tail -1 > /dev/null 2>&1
     if [ $? -eq 0 ] ;then
          chmod +rwx $bindir/dummy2.sh
          lastfile=`ls -1rt /opt/apps/script/data/check.txt*|tail -1` > /dev/null 2>&1
          cp $lastfile $dir/backup/
          cp $lastfile $datadir/IPLIST.txt
     else
         chmod -rwx $bindir/script_check2.sh
         exit 0
     fi
done

Its giving me infinite loop. The check.txt is not in the /opt/apps/script/data/ directory, so I thought I wanted it to do chmod -rwx $bindir/script_check2.sh and exit script straight away.

please help.[GOOGLE]

[/GOOGLE]

The loop is infinite since you you used : to evaluate as true which will always be true.
Maybe add a "if -e check.txt"-test instead checking if the file is ls'able and reading it's last line.
Also if you don't want a loop at all, don't use it.
If it is a thing you only want to do once or by cron then no infinite while loop is needed.
If you want to use it like a demon, a permanent process that is running, checking and doing your commands on the files, then stick with the infinite while loop.

Also add CODE-tags to your forum posts in future to enhance readability and to keep formatting like indentition etc. next time. I added CODE-tags to your post so you can see what I mean.

Okay, I need to use the loop because the script are in chained list of process flow, and I execute the script to check if the file is available or not, if it doesnt then exit. I did ls -1rt because I need to process the file from the latest file available, and once its processed on the next flow, another script will remove the latest check.txt(timestamp) until no more available.

Would you kindly please fix my script where need be and repost the code please? I would really appreciate it thank you very much for your prompt support! :b::smiley:

I have a meeting right now - will have a look into it after the meeting. Maybe someone else does. You are right about the latest file - I misread the ls -ltr, sorry.

Assuming your script is called $bindir/script_check2.sh , changing the execute status of the script you are running will not stop the script.

To exit a while loop, use shell command "break".

else
         break
fi

BTW: There may be logic issues in the script. If a check file does exist, it will copy the file in an infinite loop.

Be aware of the return code of this line, it gives the return code of the tail command:

ls -1rt /opt/apps/script/data/check.txt*|tail -1 > /dev/null 2>&1

The return code is always 0 in this case. Maybe you can do something like:

file=`ls -1rt /opt/apps/script/data/check.txt*|tail -1 2>/dev/null`

if [ -z "$file" ]; then
  ...
fi

errmm hmm

Here is my latest update, based on ksh -x script.sh, I figured out it wont stop looping when the IP-List.txt* file is no longer available (all removed), instead, $lastfile variable was assigned to a null value. I wanted it to stop when there is no more IP-List.txt* file :(. Also I wanted to do

sort -n $datadir/IP.txt | uniq >> $datadir/IPLIST.txt at the end of the loop before it restarts loop again but it wont work :P. Please help :frowning:

#!/bin/sh

bindir='/opt/apps/script/bin'
datadir='/opt/apps/script/data'
dir='/opt/apps/script'

while : ; do

file=`ls -1rt /opt/apps/script/IP-List.txt*|tail -1 > /dev/null 2>&1`
if [ -z "$file" ] ;then

lastfile=`ls -1rt /opt/apps/script/data/IP-List.txt*|tail -1` > /dev/null 2>&1
cat $lastfile >> $datadir/IP.txt
rm $lastfile

else
break
exit 0
fi
done

You like todo ls again if file not exist ? Or like todo if exist ?
Now it looks that when file is empy, you make ls again and cat+rm for file ""

if [ "$file" != "" ] ; then # filename is not empty string , so cat+rm lastfile

else

  • you have break before exit, so never use exit - break is enough

ls -1t | head -1
is faster = process first line, not last

...
done
# line is ip ? nn.nn.nn.nn ?, then use sort like: (sort has also -u = uniq)
sort -u -t . -k 1,1n -k 2,2n -k 3,3n -k 4,4n  $datadir/IP.txt >> $datadir/IPLIST.txt