Go2Linux posted a guide about debugging shell scripts
Debugging is an important part of programming in any language, and so it is the same for Bash, one of the most easy way to debug is to run the program line by line.How to debug shell scripts
This way you can check what is happening with each line, and you can then find where is the error.