I just notice an example from Roland for aborting a script with an error
message:
... || { print -u2 "failure" ; exit 1 ; }
I also see a lot of four liners based on if/then/fi in scripts.
For years I've borrowed warn() and die() from perl, defined as shell functions.
Maybe it's time versions of these are added as builtins to ksh?
#
# die() exits the script with a message. The exit code defaults to 1
# An optional positive integer $1 will be stripped from the message
# and used as the exit code.
#
function die {
# usage: die [ status ] message ...
typeset status=$1
[[ $status == [1-9]*([0-9]) ]] && shift || status=1
print -u2 "$*"
exit $status
}
function warn { print -u2 "$*"; return 1; }
To create an orthogonal set, I've also defined tell() and bye()
which return or exit with a zero (success) value:
function tell { print -u2 "$*"; return 0; }
function bye { print -u2 "$*"; exit 0; }
At lazy times I've also used good old exit with some trickery
... || exit 11 $(print -u2 "uh-oh, something went wrong.")
Nice thing is that it's visually the same as die. When we use the
block { } syntax, the exit command is hidden at the end of the line.
"Important things are small, and should come first" is the design
principle here.
In this case, the word "die" or "exit" is the first thing after
the failing code. We know immediately the script is bailing out here.
The exit value is next, and provides us with a unique identifier
into the code. The message is a side effect, and comes last.
Cheers,
Henk
_______________________________________________
ast-users mailing list
[email protected]
https://mailman.research.att.com/mailman/listinfo/ast-users