[NTLUG:Discuss] bash question...beating a dead horse...

Fred James fredjame at concentric.net
Tue Mar 5 12:18:16 CST 2002


My questions revolve around the shell script, and text, files below - I 
know I have already received some good answers on this but I guess I am 
just slow or something.

The script contains 3 loops, each of which increment a counter that has 
been initialized to zero.  When I run this on my Red Hat 7.1 laptop:
(1) the count is 10 outside (after) the first loop
(2) the count is zero outside (after) the second loop.
(3) the count is 4 outside (after) the third loop.

So, if the counter in the second loop is lost outside that loop because 
the loop is executed in a subscript (is that the explanation?):
(1) How could I get the value of the counter for use after the second 
loop competes?
(2) If I assigned the values read into an array, would the contents of 
the array be available after the loop completes - if not normally, is 
there a way to do so?

Thanks in advance for your thoughts and efforts.


Script file:
==========
#! /bin/bash
# 
count.sh

Count=0
while test $Count -lt 10
do
	echo "Inside first loop $Count"
	Count=`expr $Count + 1`
done
echo "Outside first loop $Count"

Count=0
cat fun.txt | while read animal color
do
	echo "Inside second loop: animal:$animal color:$color $Count"
	Count=`expr $Count + 1`
done
echo "Outside second loop animal:$animal color:$color $Count"

Count=0
while read animal color
do
	echo "Inside third loop: animal:$animal color:$color $Count"
	Count=`expr $Count + 1`
done < fun.txt
echo "Outside third loop animal:$animal color:$color $Count"
exit





More information about the Discuss mailing list