[NTLUG:Discuss] Simple bash shell question

Chris Cox cjcox at acm.org
Fri Mar 30 15:59:54 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

In a typical bourne shell like environment, the while loop
is going to be a sub-shell of the script... so fcount
is valid in that context only.

How about:

for filename in *.mpg; do
	fcount=$(expr $fcount + 1)
	# Or if we are assuming bash... fcount=$(($fcount + 1))
	echo $fcount
done
echo $fcount

Will that work for you instead?



More information about the Discuss mailing list