Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Safe scripting



On Wed, 21 Jan 2009, Richard Hartmann wrote:

Atom's solution is rather magic, but works very well. If you want to be
portable, you can also do

cd foo       || exit 10
rm bar       || exit 11
cd foobar    || exit 12
rm foobarbar || exit 13

That is very basic and can be extended, for example with

cd foo       || my_handler 'error foo' '10'

function my_handler() {
	echo $1 $2
}

etc, etc.
===================

you can also sprinkle some magic on that, and it still looks portable...

error () {
	print "something went wrong on line ${1}" >&2
	exit $[ (${1} - 1) % 127 + 1 ]
}
 cd foo		|| error ${LINENO}
 cd foobar	|| error ${LINENO}
 cd baz		|| error ${LINENO}

the extra math in the exit value would help identify the line that failed using the exit status, but keep the value within a sane range. for scripts shorter than 127 lines "exit ${1}" would suffice.

i ~think~ 127 is the correct value for the mod operation, keeping the exit status within the range of 1-127, and than wrapping. i'm sure someone here can point out if 128 or even 255 is a reasonable value.


--
        ...atom

 ________________________
 http://atom.smasher.org/
 762A 3B98 A3C3 96C9 C6B7 582A B88D 52E4 D9F5 7808
 -------------------------------------------------

	"It is well enough that people of the nation do not
	 understand our banking and monetary system, for if
	 they did, I believe there would be a revolution before
	 tomorrow morning."
		-- Henry Ford



Messages sorted by: Reverse Date, Date, Thread, Author