# infinite while do loop problem

**URL:** <https://community.unix.com/t/infinite-while-do-loop-problem/244777>\
**Category:** Shell Programming and Scripting\
**Created:** [July 16, 2009, 7:43am UTC](https://community.unix.com/t/infinite-while-do-loop-problem/244777 "2009-07-16T07:43:01Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![tententen](https://community.unix.com/letter_avatar/tententen/32/5_5575768a8748004e209b776fc1b2916d.png) [@tententen](https://community.unix.com/u/tententen)\
**Post date:** [July 16, 2009, 7:43am UTC](https://community.unix.com/t/infinite-while-do-loop-problem/244777/1 "2009-07-16T07:43:01Z")

</div>

hi all,

this is how my scrip looks like

```nohighlight
#!/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]

```nohighlight

```

[/GOOGLE]

---

<div class="post-metadata">

**Author:** ![zaxxon](https://community.unix.com/user_avatar/community.unix.com/zaxxon/32/660_2.png) [@zaxxon](https://community.unix.com/u/zaxxon)\
**Post date:** [July 16, 2009, 7:54am UTC](https://community.unix.com/t/infinite-while-do-loop-problem/244777/2 "2009-07-16T07:54:25Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![tententen](https://community.unix.com/letter_avatar/tententen/32/5_5575768a8748004e209b776fc1b2916d.png) [@tententen](https://community.unix.com/u/tententen)\
**Post date:** [July 16, 2009, 8:03am UTC](https://community.unix.com/t/infinite-while-do-loop-problem/244777/3 "2009-07-16T08:03:48Z")

</div>

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:😃

---

<div class="post-metadata">

**Author:** ![zaxxon](https://community.unix.com/user_avatar/community.unix.com/zaxxon/32/660_2.png) [@zaxxon](https://community.unix.com/u/zaxxon)\
**Post date:** [July 16, 2009, 8:07am UTC](https://community.unix.com/t/infinite-while-do-loop-problem/244777/4 "2009-07-16T08:07:17Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![methyl](https://community.unix.com/letter_avatar/methyl/32/5_5575768a8748004e209b776fc1b2916d.png) [@methyl](https://community.unix.com/u/methyl)\
**Post date:** [July 16, 2009, 8:07am UTC](https://community.unix.com/t/infinite-while-do-loop-problem/244777/5 "2009-07-16T08:07:48Z")

</div>

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

> [@](#):
>
> else  
> chmod -rwx $bindir/script\_check2.sh  
> exit 0  
> fi

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

```plaintext
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.

---

<div class="post-metadata">

**Author:** ![Franklin52](https://community.unix.com/user_avatar/community.unix.com/franklin52/32/500_2.png) [@Franklin52](https://community.unix.com/u/Franklin52)\
**Post date:** [July 16, 2009, 8:23am UTC](https://community.unix.com/t/infinite-while-do-loop-problem/244777/6 "2009-07-16T08:23:59Z")

</div>

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

```nohighlight
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:

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

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

```

---

<div class="post-metadata">

**Author:** ![tententen](https://community.unix.com/letter_avatar/tententen/32/5_5575768a8748004e209b776fc1b2916d.png) [@tententen](https://community.unix.com/u/tententen)\
**Post date:** [July 16, 2009, 8:24am UTC](https://community.unix.com/t/infinite-while-do-loop-problem/244777/7 "2009-07-16T08:24:52Z")

</div>

errmm hmm

---

<div class="post-metadata">

**Author:** ![tententen](https://community.unix.com/letter_avatar/tententen/32/5_5575768a8748004e209b776fc1b2916d.png) [@tententen](https://community.unix.com/u/tententen)\
**Post date:** [July 22, 2009, 7:33am UTC](https://community.unix.com/t/infinite-while-do-loop-problem/244777/8 "2009-07-22T07:33:13Z")

</div>

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 ☹

```nohighlight
#!/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

```

---

<div class="post-metadata">

**Author:** ![kshji](https://community.unix.com/user_avatar/community.unix.com/kshji/32/15661_2.png) [@kshji](https://community.unix.com/u/kshji)\
**Post date:** [July 22, 2009, 9:20am UTC](https://community.unix.com/t/infinite-while-do-loop-problem/244777/9 "2009-07-22T09:20:17Z")

</div>

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

```nohighlight
...
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 

```
