I agree but I don't know much bash/shell scripting so I'm using what I know.
Also shell scripting is not very flexible.In particular,what you wrote there
in brackets "do whatever with $size here" will probably not be accomlished
very easily using shell scripting.
Actually, it is indeed accomplished very easily. The fact that it's so complicated in your script is a demonstration that you're coercing a square peg into a round hole.