Exit statement ignored inside KSH function


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Exit statement ignored inside KSH function
# 1  
Old 02-06-2009
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:

Code:
#!/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.

Last edited by Franklin52; 02-06-2009 at 06:41 AM.. Reason: adding code tags
# 2  
Old 02-06-2009
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
# 3  
Old 02-06-2009
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?

Code:
#!/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:

Code:
func1
504026
635114
func2 504026 635114
func1
504026
635114
func2 504026 635114

Cheers,

--
Greg.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Function - Make your function return an exit status

Hi All, Good Day, seeking for your assistance on how to not perform my 2nd, 3rd,4th etc.. function if my 1st function is in else condition. #Body function1() { if then echo "exist" else echo "not exist" } #if not exist in function1 my all other function will not proceed.... (4 Replies)
Discussion started by: meister29
4 Replies

2. UNIX for Beginners Questions & Answers

How to use exit status of two commands in if statement ?

I am trying to write a shell script, which looks like #!/usr/bin/env bash #set -e RED="`tput setaf 1`" GREEN="`tput setaf 2`" BLUE="`tput setaf 4`" NORM="`tput sgr0`" pushd ${MY_GIT_TOP}/body/Ue/test >/dev/null #MY_GIT_TOP is set my by gitenv make test_trinity_svp pushd... (8 Replies)
Discussion started by: Sekhar419
8 Replies

3. Shell Programming and Scripting

Need help on awk for printing the function name inside each function

Hi, I am having script which contains many functions. Need to print each function name at the starting of the function. Like below, functionname() { echo "functionname" commands.... } I've tried like below, func=`grep "()" scriptname | cut -d "(" -f1` for i in $func do nawk -v... (4 Replies)
Discussion started by: Sumanthsv
4 Replies

4. Shell Programming and Scripting

After exit from function it should not call other function

Below is my script that is function properly per my conditions but I am facing one problem here that is when one function fails then Iy should not check other functions but it calls the other function too So anyone can help me how could i achieve this? iNOUT i AM GIVING TO THE... (1 Reply)
Discussion started by: rohit22hamirpur
1 Replies

5. UNIX for Advanced & Expert Users

Cannot exit from a function?

Hi, Is there a way to exit from a subcommand, which is a function in my example below? #!/bin/ksh function exitFunction { if ]; then echo "success" elif ]; then echo "failed" exit 1 # the exit problem fi exit 0 } ... (2 Replies)
Discussion started by: chstr_14
2 Replies

6. UNIX for Dummies Questions & Answers

Using 'diff' exit status in an if statement

is there a way to compare two files using diff (ex: diff 1.txt 2.txt) in an if statement? I read that the exit status of diff is 0 if the files contain the same content. 1 if they're different. So what I am attempting is basically: if ; then echo "they're the same" else ... (2 Replies)
Discussion started by: SoVi3t
2 Replies

7. Shell Programming and Scripting

How to call an sql script inside a while statement in KSH

Hi all, I'm trying to run an sql inside a loop which looks like this #!bin/ksh while IFS=, read var1 var2 do sqlplus -s ${USERNAME}/${PASSWORD}@${ORACLE_SID} << EOF insert into ${TABLE} ( appt_date ) values ( '${var1 }' ); ... (6 Replies)
Discussion started by: ryukishin_17
6 Replies

8. Shell Programming and Scripting

Ksh script function, how to "EXIT 2" without killing the current process?

Hi, Using AIX 5.3 and Ksh. />ls -al /usr/bin/ksh -r-xr-xr-x 5 bin bin 237420 Apr 10 2007 /usr/bin/ksh /> I recently started working for a new employer. I have written UNIX K-Shell scripts for many years and have never had this particular issue before. Its perplexing me. I have... (2 Replies)
Discussion started by: troym72
2 Replies

9. Shell Programming and Scripting

Help with EXIT command inside if

Hi All, I want to use EXIT command in a particular scenario. In my script,its like that:- if($vairable -eq 1);then do this thing do this do this fi if($var2 -eq 1);then do this do this fi like this there are three four if conditions. now I want is that if second condition is... (2 Replies)
Discussion started by: aruncser
2 Replies

10. Programming

Usage of exit() inside a signal handler

Is it ok to use exit() inside a signal handler? I catch SIGUSR1 in a signal handler and I try to close a file and then exit. The result is inconsistent. Sometimes the process exit and sometimes it returns to the original state before the signal handler was invoked. Perhaps exit is not legal in... (8 Replies)
Discussion started by: Tuvia
8 Replies
Login or Register to Ask a Question