Hi, I have two shell scripts that do the same thing, but are written differently. The first one works:
size=0
pattern=foo
for file in *
do
[ ! -f $file ] && continue
if grep $pattern $file > /dev/null
then
tsize=`cat $file |wc -c`
size=`expr $size + $tsize`
echo "size is: $size"
fi
done
echo "Total size of files containing $pattern is: $size"
With the output of
$ ./foofinder
size is: 34
size is: 77
size is: 223
size is: 478
size is: 758
size is: 855
Total size of files containing foo is: 855
But, I then have this script:
pattern=foo
total=0
ls -l | while read j1 j2 j3 j4 fsize j5 j6 j7 fname
do
[ ! -f $fname ] && continue
if grep $pattern "$fname" > /dev/null 2>%1
then
total=`expr $total + $fsize`
echo "size is: $total"
fi
done
echo "Total size of files containing $pattern is: $total"
With the output of
$ ./foofinder2
size is: 34
size is: 77
size is: 223
size is: 478
size is: 758
size is: 855
Total size of files containing foo is: 0
Basically, I want to know why the second script returns the line:
Total size of files containing foo is: 0
, when the last known value of the $size variable is 855, but the first script gets it right? Why should they be any different?
Your help with this would be greatly appreciated, as I am just new to learning shell scripting, and I would like to know why they are producing different results.
Thanks!