Maybe because you're running the code with /bin/sh and in compatibility mode with the old Bourne Shell the syntax is not correct. Anyway, the $[...] construct for integer expansion is deprecated and it should be avoided in favor of the arithmetic operator ((...)).
On the other hand, if you're really writing a script for the Bourne shell, better to use expr.
|