Alright, get tips from the master.
Also, I don't have a linux machine but when making scripts(for android) I just use "root explorer's text editor to make the script itself and make corrections if needed and use terminal to test it. This way errors are addressed real time and making the corrections are far more efficient.
Like what ruigui has suggested, try making a bunch of "echo" loops, to know that all your functions work correctly and link's to each other. When everything's ok, then you can add the real commands and test them one by one.
Also, I don't have a linux machine but when making scripts(for android) I just use "root explorer's text editor to make the script itself and make corrections if needed and use terminal to test it. This way errors are addressed real time and making the corrections are far more efficient.
Like what ruigui has suggested, try making a bunch of "echo" loops, to know that all your functions work correctly and link's to each other. When everything's ok, then you can add the real commands and test them one by one.