Please post your while loop. Looking quickly at your if statement, I have to wonder about the usage of $1 in that location, also I don't think your usage of $nick is correct either.
I should be able to verify my hunches by reviewing your while loop, and then be able to assist you, including showing what you're doing wrong.