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.