Bash script for files with spaces in the filenames

Mathieu Chouquet-Stringer ml2news at free.fr
Thu Feb 16 14:59:28 UTC 2006


steveo at syslang.net ("Steven W. Orr") writes:
> Just be careful with this construct. It may do what you want but be aware 
> of what it won't do:
> 
> cnt=0
> find | while read foo 
> do
>   echo "File name $cnt = $foo" 
>   cnt=$(( cnt + 1 ))
> done
> echo $cnt
> 
> This will print out an incrementing counter in the loop but it will 
> print out a 0 for the final echo. That's because the instance of cnt in 
> the loop is in a seperate process from the instance out of the loop. They 
> are seperate variables. It's the pipe that creates the child.

Correct if you want to do something like that you need a construct similar
to this one:
cnt=0
find > /tmp/somefile
while read foo
  echo "File name $cnt = $foo" 
  cnt=$(( cnt + 1 ))
done < /tmp/somfile
echo $cnt

-- 
Mathieu Chouquet-Stringer
    "Le disparu, si l'on vénère sa mémoire, est plus présent et
                 plus puissant que le vivant".
           -- Antoine de Saint-Exupéry, Citadelle --




More information about the fedora-list mailing list