> > I would like to know if it is possible to trap errors caught by a ksh
> > shell, similar to the way you can trap signals with the "trap" special
> > command. In the case I encountered, I was dynamically generating
> > shell variables from data within a user-supplied file. The data in
> > the file had some garbage in it which caused me to generate a shell
> > variable name with an unaccceptable character in it. The shell put
> > out an error message
> > syntax error at line 4
> > and kept going (which caused the whole process to eventually fail much
> > further down the line). I want to trap that condition (and any other
> > ksh-detected error) in my high-level script and get control when any
> > such situation occurs within it or any sub-script, so I can handle the
> > situation gracefully.
> > I have corrected this particular problem (by validating the
> > user-supplied data), but I have no general solution to the problem of
> > trapping any arbitrary error caused by dynamically generated shell
> > code in a set of scripts.
> > To clarify my question, by analogy I want to do what you can do within
> > modern languages which support exceptions. For example, in C++, or
> > JAVA, or C#, at any point in my code I can set a "try ... catch" block
> > which can trap any exception not handle in a lower level. I would
> > like something similar in ksh.
> > Reply by email; I'll summarize and post.
> The usual rules of consideration tend to favor a solution whereby the original
> poster puts in at least 10% of the effort that the (unpaid) consultant expends
> to write it, in the reading of it. Also, this way, many more people benefit
> from the developing process of solving it.
> Place at the top of your script:
> function _myerror
> { print -r error at line number $1
> print -r return value is $2
> }
> trap '_myerror $LINENO $?' ERR
> Whenever there is an error, you may modify the function to report, log, and/
> or terminate the script.
> =Brian
Thanks, but this does not reslove my problem. I am not interested in
trapping non-zero exit statuses globally, which is what "trap ... ERR"
does. I want to trap run-time errors that don't necessarily generate
return values, such as syntax errors, and specifically syntax-type
errors from dynamically generated shell code. (Any syntax errors from
my static shell code I would catch during debugging.)
For example, the following shell script
#! /bin/ksh
trap 'print CAUGHT ERROR VIA TRAP AT $LINENO, RETURN CODE $?' ERR
let "abc=0"
aa=';abc'
eval $aa=newvalue
print see if I get here
generates the following output (invoked via "ksh -xv):
trap 'print CAUGHT ERROR VIA TRAP AT $LINENO, RETURN CODE $?' ERR
+ trap print CAUGHT ERROR VIA TRAP AT $LINENO, RETURN CODE $? ERR
let "abc=0"
+ let abc=0
+ print CAUGHT ERROR VIA TRAP AT 4, RETURN CODE 1
CAUGHT ERROR VIA TRAP AT 4, RETURN CODE 1
aa=';abc'
+ aa=;abc
eval $aa=newvalue
+ eval ;abc=newvalue
test[7]: syntax error at line 1 : `end of file' unexpected
The "trap" caught the non-zero return code from the "let" (which is
not an error), and did not trap the error I want to trap, namely the
"eval" error. In my real-world case, I am reading the value assigned
to "$aa" from a user file, so the error is in the input file, not my
script's code. My script's only error was not validating the data
read from input file, which I have corrected. Also note that my
real-world script is being run non-interactively, so the fact that ksh
outputs a message to stderr does me no good.
In retrospect I should have included the above example in my initial
query.
I suspect that what I'm trying to do is not doable, but I'd really
appreciate it if it was.