Minor shell cleanups

Remove some duplication, and determine the script name from $0 instead of hardcoding it.