Ell
Ell

Reputation: 947

Trap ERR strange behaviour (infinite loop) depending on how function is defined

Hope somebody can help with this - not sure if this is a known bug with ksh93?

#!/bin/ksh93
_errorCheck()
{
        LINENO=$1
        print "Error on or near line $LINENO - please check error log"
        exit 1
}
trap '_errorCheck ${LINENO}' ERR

false

This ouputs:

 Error on or near line 10 - please check error log

However...if the function is defined using the word function instead of ():

#!/bin/ksh93
function _errorCheck 
{
        LINENO=$1
        print "Error on or near line $LINENO - please check error log"
        exit 1
}
trap '_errorCheck ${LINENO}' ERR

false

This then outputs:

Error on or near line 10 - please check error log
Error on or near line 10 - please check error log
Error on or near line 10 - please check error log
Error on or near line 10 - please check error log
Error on or near line 10 - please check error log
Error on or near line 10 - please check error log

What seems to be an infinite amount of times...

Can anyone explain this behaviour?

As requested: KSH_VER=Version M-12/28/93e

Upvotes: 3

Views: 207

Answers (2)

Funkynene
Funkynene

Reputation: 63

Using function f {...} and f() {...} it's actually different in ksh. You can check this answer which explains very well what you are experimenting. https://unix.stackexchange.com/questions/73750/difference-between-function-foo-and-foo

Upvotes: 1

Henk Langeveld
Henk Langeveld

Reputation: 8446

You can add a line

trap - ERR

at the top of either function to reset the trap after it first triggers.

For me the second function shows expected behaviour.

Anyway, you experimented and found something that works. That's all that matters.

Upvotes: 0

Related Questions