while read in bash

der.hans plug-devel@lists.PLUG.phoenix.az.us
Sun May 27 14:42:01 2001


moin, moin,

I'm trying to loop over some input, toss some info in a variable, then
access that info after the while loop is finished. Unfortunately, this isn't
working. A while loop by itself will work fine, but with read it doesn't.

Here's an example script. Note that preassignment of the sought after
variable doesn't affect the while read, but it does affect the condition
clause of the while loop.

Anyone know how to make the info available beyond the done? Putting it in a
function did not help. Reading from keyboard and not a pipe, however, does
obtain the behavior I'm expecting.

--- cut here ---

liases2root="fred "
count=0

# while read nickname destination otherdests
grep root /etc/aliases | while read nickname destination otherdests
do
        if [ "$destination" = "root" ] ; then
                # echo $nickname
                aliases2root="$aliases2root $nickname"
                echo $aliases2root
        fi      
done    

while [ $count -lt 5 ]
do
        count=`expr $count + 1`
done

echo "<$aliases2root> <$count>"
--- cut here ---

ciao,

der.hans
-- 
# der.hans@LuftHans.com home.pages.de/~lufthans/ www.DevelopOnline.com
#  Help Jerry Lewis stamp out M$...oops that's MDA - der.hans