redirect output of for loop to file and screen

Hi ... iam trying to redirect the individual steps of a for loop to the screen... and at the end the whole 'for' loop output redirected to a file.
for filename in `ls -l *.xml'
do
echo
echo ---------------------
echo TESTING $filename
echo ---------------------
echo
done >loop.out

with this script the output writes to the file... but the individual steps do not show on the screen. Please help me achieve it.

---------------Original Message---------------
>Hi ... iam trying to redirect the individual steps of a for loop to the screen... and at the end the whole 'for' loop output redirected to a file.
>for filename in `ls -l *.xml'

Not only do you not need ls, you will not be populating $filename with filenames, but with each word output by 'ls -l'. Even leaving off -l will break the script if any filenames contain whitespace or other pathological characters. Use:

In addition to what has already been said (about wrongly populating the variable and tee-ing to the o/p file), you can redirect individual outputs of commands within the loop by specifying >/dev/tty with them.

for filename in *.xml < this code doesn't works .And it's a little unsafe.

Better try this :

[[ -s loop.out ]] && rm -f loop.out

for filename in $(find . -type f -name *.xml -print 2>/dev/null)
do

echo "\n\n\t Testing file\n\n"

echo "${filename}" >> loop.out

# This appends each output to a single file # until the end of the loop .

done

Try to write into another tty it's not always possible , I.E if in your profile the var mesg it's setted wiht the value "n" you 'll not able to write in the tty.
if you want to do that ,you 'll include this lien in your profile.

In what way does it not work? What is unsafe about it? It is, in fact, the safest way to loop through files in a directory.

>Better try this :
>
>
>[[ -s loop.out ]] && rm -f loop.out

That syntax is not portable.

[ -s loop.out ] && rm -f loop.out

>for filename in $(find . -type f -name *.xml -print 2>/dev/null)
>do

That will break if there are spaces or other pathological characters in any of the filenames.

>echo "\n\n\t Testing file\n\n"
>
>echo "${filename}" >> loop.out
>
># This appends each output to a single file # until the end of the loop .
>
>done
>
>Try to write into another tty it's not always possible , I.E if in your profile the var mesg it's setted wiht the value "n" you 'll not able to write in the tty.
>if you want to do that ,you 'll include this lien in your profile.
>
>mesg y
>
>Thanks and regards.
>
>