bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Nested for loop inside a while loop


From: Alex Lanstein
Subject: Nested for loop inside a while loop
Date: Sun, 14 Nov 2004 22:25:41 -0500

I don't really understand why this hasnt been fixed or reported yet,
but when I need a for loop inside a whileloop, all the variabled
created or edited inside the for loop are local, not global.  Here is
example code:

count=1
cat temp5 | while read num
do
if [ "$num" -ge "$count" ]; then
echo "$num" "is bigger than" "$count"
count="$num"
echo "$count" "is the new counter"
else
echo "$count" "is already bigger than" "$num"
fi
done
echo "$count" "is the temp counter now"

when im echoing it inside the loop $count is what it should be (5327)
but as soon as it exits, $count is 1 again

However, if ALL I do is edit the 'cat temp5 | while read num' to 'for
num in $( cat temp5 )', it works perfectly, $count gets echoed as 5327
instead of 1.

If a comment does get added to the code, please give Alex Lanstein and
Twans credit for it.

Thanks!
Alex Lanstein




reply via email to

[Prev in Thread] Current Thread [Next in Thread]