A common solution is to wrap all code within a function. This way nothing gets executed until the last line, the one that calls the function, is executed.
function main () {
# all code goes here
}
main