Exit statement ignored inside KSH function

Hi All,

I have multiple functions in my script and I'm trying to capture stdout from some of them, but I also do some error checking in them (in the functions that output something to their stdout that needs capturing) and I need to be able to end the entire script with an error message.

KSH version I'm using is the default that comes with AIX 6.1 (6100-02-02-0849), which is M-11/16/88f.

Consider the following example:

#!/usr/bin/ksh
func2() {
        echo "func2"
        # let's say some error occured here
        # and I need to stop the entire script
        exit 1
}
func1() {
        echo "func1"
        func2
}
func1 # 1st call
func1 # 2nd call

As expected, the output of this script is:
 
func1
func2

Now consider this example:
 
#!/usr/bin/ksh
func2() {
        echo "func2"
        # let's say some error occured here
        # and I need to stop the entire script
        exit 1
}
func1() {
        echo "func1"
        out=`func2`
        echo $out
}
func1 # 1st call
func1 # 2nd call

The output of this one is:
 
func1
func2
func1
func2

So both 'func1' calls at the bottom of the script are processed. Because I'm capturing the stdout of 'func2' function, the exit seems to be treated as return. Same problem exists when I try to re-direct stdout (using pipe, i.e. a call like this: func2 | tr '[a-z]' '[A-Z]' from func1 has the same unintended effect), basically, every time I tap into stdout, the exit doesn't stop the entrie script. I'd also prefer not to use temporary files or anything like that (that would also be redirecting stdout so it may not work either, but I haven't tried it yet, as I consider it untidy). I would also like to avoid checking for retrun code of the function outside it, I do all error checking and messaging inside the function that outputs things on stdout, so the neatest approach is to simply exit the script from that function if critical error occurs. How do I make the exit statement always terminate the script?

Thanks in advance,

--
Greg.

The backticks fork a new proces (child), so the exit statement closes the child proces not the current shell.
Better is to check the return code of the function with $? to determine the behaviour of your script.

Regards

Thanks for the reply.

If that's the case I should be able to use

kill -TERM $PPID

from func2 (assuming TERM signal isn't trapped, and it isn't in my case).

But this example shows the same PID and PPID for both functions, as if real fork() syscall weren't actually used here or possibly $$ and $PPID are carried from the parent shell. What have I missed?

#!/usr/bin/ksh
func2() {
        echo "func2"
        # let's say some error occured here
        # and I need to exit
        echo $$
        echo $PPID
        exit 1
}
func1() {
        echo "func1"
        echo $$
        echo $PPID
        out=`func2`
        echo $out
}
func1 # 1st call
func1 # 2nd call

It returns this:

func1
504026
635114
func2 504026 635114
func1
504026
635114
func2 504026 635114

Cheers,

--
Greg.