Trapping ksh syntax errors in dynamically created code

Trapping ksh syntax errors in dynamically created code

Post by Moshe Rabinowi » Fri, 28 Mar 2003 09:02:27



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.

 
 
 

Trapping ksh syntax errors in dynamically created code

Post by b.. » Fri, 28 Mar 2003 11:09:27



> 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

Quote:}

trap '_myerror $LINENO $?' ERR

Whenever there is an error, you may modify the function to report, log, and/
or terminate the script.

=Brian

 
 
 

Trapping ksh syntax errors in dynamically created code

Post by Moshe Rabinowi » Sat, 29 Mar 2003 04:48:35




> > 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.

 
 
 

1. ksh - problem with dynamically created pipe using 'eval'

In a ksh script i want to dynamically create a pipe, which looks as
follows:

    ls -lR $1 | awk '/^d/' $filter $sort

If i define either filter or sort (sort="| sort") i get an error from
awk (awk: Cannot find or open file |.). This is no surprise. I thought
the following line would solve the problem:

    eval ls -lR $1 | awk '/^d/' $filter $sort

I was very surprised to find that this was no solution, i got the same
error message from awk. However the following small change works fine:

    ls -lR $1 | eval awk '/^d/' $filter $sort

Is this the expected behaviour of ksh or a problem with the
implementation on my site? Can someone explain to me what happened?

+----------------------------------------------------------------------+
|  Guenther Steinmetz           Mail:  Postfach 810247  81902 Muenchen |
|  Quality Consultant           Voice: (49) 89-9591-2892               |
|  Digital Equipment GmbH       Fax:   (49) 89-9591-2575               |
+----------------------------------------------------------------------+

2. User lists in a Microsoft Network served by a linux machine

3. KSH: error when redirecting in subshell: stty: syntax error

4. What FS is the best fit for an embedded device

5. Return codes in trap handlers in ksh

6. Sound card problems with recent kernels

7. access.conf syntax error without a syntax error

8. Hep p ko lp on f77

9. ksh: trap '...' exit int ... or just trap '...' exit?

10. ksh and trap...(how to release trap?).

11. ksh - trapping errors in functions?

12. ksh: Syntax error

13. ksh syntax error: if [test] then ftp <! ...!