Explanation for this small shellscript

sum 1 = 0
sum2 = 0
cnt = 0
while read W
  echo $W | grep A >/dev/null && cnt= 'echo -n $W | wc -c' && let sum1=sum1+cnt
  echo $W | grep i >/dev/null | cnt='echo -n $W | wc -c && let sum2=sum2+cnt
echo $sum1
echo $sum2

Adams Aspirin Asimov Brunner Clarke
Dick Gibson Grimwood Haldemann Herbert
Lem Niven Simmons Strugatzki Verne

The solution for this is according to the tutor
cnt’echo -n $W | wc -c’ 3 times
Line 8 should be 7 times
sum 1 = 17 times
sum 2 = 42 times

My problem is I dont understand how he comes to this count,

I understand that the first echo has something to do with the letter ‘A’
and the 2nd echo is looking for ‘i’.
sum1=sum1+cnt means that the value of cnt gets added to sum1. Last lines are just printing out the value of sum1 and sum2.

Can someone explain me the middle part please?


Maybe you should zeroise cnt, just before the ‘done’ statement, ready for the next iteration.
Also you are missing a quote mark just after wc -c, on the second long line…


Can you state your problem clearly? Are you trying to count ‘A’ and ‘i’ from input files?