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.