Problem with file redirection in while

I've got a problem with file redirection in bash, in loop while (in my script done < bufor.txt). In every consecutive iteration there's a possibility that to the bufor.txt will be added some lines. Unfortunately, in loop, added lines are ignored (for example: bufor.txt has 5 lines, in 3rd iteration scripts add 2 lines to the file, but the loop ends after reading 5th line). Have you got any idea how to fix it? If the number of lines in bufor.txt changes, how to change dynamically number of iterations of loop?

Here's my code (there is no any huge functionality in it, it should look through files and put names included in them [*.h files] in bufor.bf). I want it to work recursively:

#!/bin/bash
    # kk9
    # 21-11-2012
    # makefile generator
     
    sed -n -e '/^#include "/p' $1 > bufor.b1
    sed -e 's/#include[[:space:]]"//g' bufor.b1 > bufor.b2
    sed -e 's/.h"/.c/' bufor.b2 > bufor.b3
     
    # in bufor.b3 there are now files included in main ($1) file
    cat bufor.b3 >> bufor.bf
     
    # deleting '\r'
    sed -i 's/\r$//' bufor.bf
     
    rm bufor.b1
    rm bufor.b2
    rm bufor.b3
     
    touch analized.bf
     
    while read WIERSZ; do
                    echo "---------------- $WIERSZ"
                    echo "$WIERSZ" > tmp.t
     
                    # deleting '\r'
                    sed -i 's/\r$//' tmp.t
     
                    ZMIENNA=`cat tmp.t`
     
                    echo "VARIABLE: $ZMIENNA"
                   
                    sed -n -e "/^#include \"/p" $ZMIENNA > bufor.b1
                    sed -e 's/#include[[:space:]]"//g' bufor.b1 > bufor.b2
                    sed -e 's/.h"/.c/' bufor.b2 > bufor.b3
     
                    echo "--BUFOR.B3"
                    cat bufor.b3
                    echo "--END OF BUFOR.B3"
                   
                    # checking if the file was analized
                    sed -i 's/\r$//' bufor.bf
                    cat bufor.bf > bufor.cmp
     
                    sed -i 's/\r$//' bufor.b3
                    cat bufor.b3 >> bufor.cmp
     
                    echo "--BUFOR.CMP"
                    cat bufor.cmp
                    echo "--END OF BUFOR.CMP"
                    echo "--BUFOR.BF"
                    cat bufor.bf
                    echo "--END OF BUFOR.BF"
                    echo "--sort BUFOR.CMP"
                    sort bufor.cmp | uniq
                    echo "--END OF BUFOR.CMP SORT"         
                    echo "--sort BUFOR.BF"
                    sort bufor.bf | uniq
                    echo "--END OF BUFOR.BF SORT"  
     
                    rm tmp.t
     
                    CMP1=`sort bufor.bf | uniq | tr -d '[:space:]'`
                    CMP2=`sort bufor.cmp | uniq | tr -d '[:space:]'`
     
                    echo "CMP1:"
                    echo $CMP1
                    echo "CMP2:"
                    echo $CMP2
                   
                    if [ $CMP1 = $CMP2 ]; then
                            echo "CMP1 i CMP2 ARE EQUAL"
                            rm bufor.cmp
                    else
     
                            echo "NEW FILE - CMP1 and CMP2 are not equal!"
                            cat bufor.b3 >> bufor.bf
                            sed -i 's/\r$//' bufor.bf
     
                            echo "BUFOR.BF AFTER CHANGE"
                            cat bufor.bf
                            echo "END OF BUFOR.BF AFTER CHANGE"
     
                            rm bufor.cmp
                    fi
    done < bufor.bf

I attached a package with some test files (executing /.mmf.sh main.c should return: test1.c test3.c test2.c test4.c in bufor.bf; as we can see, test2.c isn't analized in the loop).

Thanks in advance for help.

I tested a while loop in KSH and I observed additional lines added to input file are getting read!

So can you test running this script in KSH instead of BASH and see if you are getting the desired result?

EDIT: Like Scrutinizer mentioned operation performed on the file within the script will not work, but I tried modifying the file outside the script and it works for KSH

bufor.bf gets fed into the loop and is being read line by line, but at the same time all kinds of operations are performed on this file from within the loop. That will not work..

Thanks for responses.

@Scrutinizer - I wrote a simple script, which adds some lines to the file, redirected to while loop. And the number of iterations increased after modifying the file in loop. The problem is in my implementation of my actual project. In this case, as you have written - it doesn't work. But it is possible to change number of iterations, when new lines are being added to redirected file in iterations of loop.

@bipinajith - It musn't be in ksh - it's for my classes on university and it should be in bash.

Maybe you can see a solution to this problem? Maybe redirection to while is unnecessary?

Do not post classroom or homework problems in the main forums. Homework and coursework questions can only be posted in this forum under special homework rules.

Please review the rules, which you agreed to when you registered, if you have not already done so.

More-than-likely, posting homework in the main forums has resulting in a forum infraction. If you did not post homework, please explain the company you work for and the nature of the problem you are working on.

If you did post homework in the main forums, please review the guidelines for posting homework and repost.

Thank You.

The UNIX and Linux Forums.