Shell script problem

From: Sundaram Ramasamy (sun@percipia.com)
Date: Fri Feb 28 2003 - 09:21:34 EST


Hi All,

The following Bourne shell program looses the value of variable $count
outside the loop? The program should have printed the right result "3" but
it did not.
Do not suggest other ways of counting the lines in a file. That is not the
issue. The issue is
finding why a variable looses its value outside the loop.

$ cat input.txt
John Doe
Carl Hooper
Bertrand Russel

$cat t.sh
#! /bin/sh

count=0
while read oneLine
do
    count=`expr $count + 1`
    echo "Line${count}: ${oneLine}"
done < input.txt
echo "The total no. of lines in the file 'input.txt' is: $count"

$ t.sh
Line1: John Doe
Line2: Carl Hooper
Line3: Bertrand Russel
The total no. of lines in the file 'input.txt' is: 0

Thanks
SR
_______________________________________________
sunmanagers mailing list
sunmanagers@sunmanagers.org
http://www.sunmanagers.org/mailman/listinfo/sunmanagers



This archive was generated by hypermail 2.1.7 : Wed Apr 09 2008 - 23:25:54 EDT