Just out of curiosity, why are you writing a script to do this?
FreeBSD's adduser program works very well for this. I'm not sure,
but I think that you can use it non-interactively as well.
--xPhase
Comment on Slightly OT: Why? Re: Getting Next Available Userid