Have a look at the many excellent scripts at
It is time for us to nail our colours to our chests. - BBC Radio Sussex
All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.