Minor shell cleanups

Remove some duplication, and determine the script name from $0 instead of hardcoding it.
Stupid me, I'll try to fix that tonight.