As @Joe has correctly mentioned in the comment, it is best to rewrite such a dangerous script. However, if you do wish to go ahead with your current script, a rudimentary method is to wrap all your `rm` operations within `if` conditions.
So, for example:
if pwd | grep
then rm -r */;
fi;
You can modify the `grep` part according to your needs. My point is that just do a simple `grep` verification before issuing the `rm` command.