Small UNIX problem

I have a shell script that carries out an extraction process as follows:

For a given input file dummy which looks like the following:

a.txt
a 1
a 2
xnzbcnzbxcbzxncbmnzxbcmzx
546
456
45
5
6
56

a 3
b.txt
b1
b2
b3

the shell script returns two files called a.txt and b.txt followed by their corresponding content.
a.txt would contain the following content:
a 1
a 2
xnzbcnzbxcbzxncbmnzxbcmzx
546
456
45
5
6
56

and b.txt would contain the following content:
b1
b2
b3

the problem is when i run the script where the first line of dummy is not x.txt i get errors.
Example if dummy contained

asdasda
fg
grtjiogoirjtgdjkljdfslkjdf
a.txt
qwe
ert

tyu

then when i run the script i get the following ouput:

./extracter: line 8: $FILENAME: ambiguous redirect
./extracter: line 8: $FILENAME: ambiguous redirect
./extracter: line 8: $FILENAME: ambiguous redirect

but it still produces a.txt

Here is the script:

cat dummy |while read LINE
do
if [ "`echo ${LINE} |grep '.txt'$`" != "" ] ; then
FILENAME=${LINE}
continue
fi
echo $LINE >> $FILENAME
done

does anyone know how i can fix the problem.

thanks

sorry i haven't learned about shell programming but i learn a little about awk, so if you don't mind i will do it in awk. i modified earlier post in another thread so it give out the name of your file instead of number

gawk '/\.txt$/ { fn=$0;next} {print > fn}' < A

$ cat A
a.txt
a 1
a 2
xnzbcnzbxcbzxncbmnzxbcmzx
546
456
45
5
6
56

a 3
b.txt
b1
b2
b3

$ gawk '/\.txt$/ { fn=$0;next} {print > fn}' < A

$ ls
A  a.txt  b.txt

$ cat a.txt
a 1
a 2
xnzbcnzbxcbzxncbmnzxbcmzx
546
456
45
5
6
56

a 3

$ cat b.txt
b1
b2
b3

of course you could fix the regex to be more specific for your needs