The first thing you should do when writing a script is to break it down into all the simple commands that you have to run to accomplish what you want.
Test the commands separately on the command line until you're sure you have their syntax and order of execution correct.
Then you can start to generalize the procedure, replacing fixed strings with variables and embedding them in loops for multiple runs.
That's it really. All a script is in the end is an automated sequential command list, with the syntax of the shell being used to generate the arguments you need for them.
Feel free to ask if you have problems with any of the individual steps.
And if you haven't seen the Bash Guide
yet, IMO it gives perhaps the best generalized rundown of all of bash
's major features.