Simon,
I must check to see if this is homework. Per the site rules homework is strictly forbiddden. However, if you are only asking for advice and not a free script, some people, like me and Peter, may offer help to you.
This looks an awful lot like a homework assignment to me though.
In the future, if you need help, please make it more "work related" questions instead of "homework related".
Peter's advice is right on, that script will do the trick, but I like to capture the error messages as well.
On the same line as the "done" put this:
2> /path/to/my/error.log
This will capture any errror messages that you may receive.