[NTLUG:Discuss] Simple bash shell question

Fred James fredjame at fredjame.cnc.net
Fri Mar 30 15:41:13 CDT 2007


. Daniel wrote:

>This is probably so elementary that maybe I shouldn't bother asking, but 
>I'd like to know.
>
>Given the following:
>----
>#!/bin/sh
>fcount=0
>ls -1 *.mpg | while read filename ; do
>    fcount=$(expr $fcount + 1)
>    echo $fcount
>done
>echo $fcount
>----
>
>why do I get the following output?
>----
>[daniel at alpha ~]$ test.sh
>1
>2
>3
>4
>0
>----
>
>I would expect:
>1 
>2 
>3 
>4 
>4
>
>Where is my thinking broken?
>  
>
Daniel
It is obviously a scope issue - but to test this a little further, I set
the initial value to 2 and got the following results:
3
4
5
2
... of course I only have 3 mpg files. No help perhaps.
Regards
Fred James




More information about the Discuss mailing list