in reply to Remove ^M using script

Read the error message.

perl -pi 's/\r\n/\n/' $a

is not the same as

perl -pi -e 's/\r\n/\n/' filename