From: David W. Hodgins on
On Thu, 12 Aug 2010 17:38:31 -0400, John Kelly <jak(a)isp2dial.com> wrote:

> #!/bin/sh
> trap "echo trap at $LINENO" ERR

[dave(a)hodgins ~]$ cat bin/traptest
#!/bin/bash

trap 'echo trap LINENO=$LINENO, BASH_COMMAND=$BASH_COMMAND' ERR

x=0
y=0
let z=x/y
[dave(a)hodgins ~]$ traptest
/home/dave/bin/traptest: line 7: let: z=x/y: division by 0 (error token is "y")
trap LINENO=7, BASH_COMMAND=let z=x/y

--
Change nomail.afraid.org to ody.ca to reply by email.
(nomail.afraid.org has been set up specifically for
use in usenet. Feel free to use it yourself.)
From: John Kelly on
On Thu, 12 Aug 2010 20:21:20 -0400, "David W. Hodgins"
<dwhodgins(a)nomail.afraid.org> wrote:

>[dave(a)hodgins ~]$ cat bin/traptest
>#!/bin/bash
>
>trap 'echo trap LINENO=$LINENO, BASH_COMMAND=$BASH_COMMAND' ERR
>
>x=0
>y=0
>let z=x/y
>[dave(a)hodgins ~]$ traptest
>/home/dave/bin/traptest: line 7: let: z=x/y: division by 0 (error token is "y")
>trap LINENO=7, BASH_COMMAND=let z=x/y


Works good with bash
------------------------------------------------------
#!/bin/sh

trapfunc () {
echo trap at $1
}

trap 'trapfunc $LINENO' ERR

let x=0
let y=0
let z=x/y

fw:~/temp# ./traptest
trap at 9
trap at 10
../traptest: line 11: let: z=x/y: division by 0 (error token is "y")
trap at 11



But not with the Interix ksh I'm trying to use
-------------------------------------------------------
#!/bin/sh

trapfunc () {
echo trap at $1
}

trap 'trapfunc $LINENO' ERR

let x=0
let y=0
let z=x/y

hj:~/temp# ./traptest
trap at 0
trap at 0
../traptest[11]: z=x/y: zero divisor
trap at 0


Alas, portability.


--
Web mail, POP3, and SMTP
http://www.beewyz.com/freeaccounts.php

From: Seebs on
On 2010-08-13, John Kelly <jak(a)isp2dial.com> wrote:
> But not with the Interix ksh I'm trying to use

Interesting!

I tried ksh, pdksh, and mksh. ksh gets the line numbers you want, pdksh and
mksh both seem to get zero.

The issue seems to be specific to $LINENO -- other variables work. Note that
it's not giving you the line number of the definition, but always-zero,
presumably they don't handle $LINENO in a trap context, which I guess I don't
find surprising -- it's not obvious that the line number on which the trap
occurred should be expanded in the context of the trap handler, because after
all, the trap handler really ISN'T on that line.

Interesting boundary case, not sure what I'd do about it. I'm not a big fan
of set -e to begin with; you might consider writing a function to provide
the trapping you want. For instance, you could write a function such that:

catch_error "Trying to do blah blah blah"

would print that as a diagnostic message for any following failures.

-s
--
Copyright 2010, all wrongs reversed. Peter Seebach / usenet-nospam(a)seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
From: pk on
John Kelly wrote:

>>No it doesn't.
>>
>>trap "echo $LINENO" ERR
>>
>>would do that.
>
> #!/bin/sh
>
> trap "echo trap at $LINENO" ERR
>
> let x=0
> let y=0
> let z=0
>
> # ./xs
> trap at 3
> trap at 3
> trap at 3
>
>
> Nice try but no cigar.

Perhaps if you use single quotes it may work better.
From: Eric on
On 2010-08-13, pk <pk(a)pk.invalid> wrote:
> John Kelly wrote:
>
>>>> ... because it reports the trap line, not what caused the trap.

>>>No it doesn't.
>>>
>>>trap "echo $LINENO" ERR
>>>
>>>would do that.
>>
>> #!/bin/sh
>>
>> trap "echo trap at $LINENO" ERR
>>
>> let x=0
>> let y=0
>> let z=0
>>
>> # ./xs
>> trap at 3
>> trap at 3
>> trap at 3
>>
>>
>> Nice try but no cigar.
>
> Perhaps if you use single quotes it may work better.

Actually, that's what I said, except the only responders to my post didn't
read it properly - I have re-inserted the bit from the original post
that makes it make sense! ("it" in the inserted bit is the single-quoted
version which was abve that).

Now it appears that some shell (pdksh?) plays around with its context
before executing the trap command so that LINENO is set to zero - somebody
trying to be too clever, I guess. As far as I can see, it is a _bug_ .

Eric
First  |  Prev  |  Next  |  Last
Pages: 1 2 3
Prev: download basics
Next: gnu-make prerequisites