How to exit the KSH functions


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users How to exit the KSH functions
# 8  
Old 12-10-2007
If I have a script that looks like

Code:
usefull()
{
    exit
}

and run it with

Code:
./scriptname

nothing much will happen

if I run it with

Code:
. ./scriptname

nothing much will appear to happen except that the function will be remembered.

now if I type

Code:
usefull

bingo, the session dissappears.

No surprises, and everything doing what it says it will do.
# 9  
Old 12-11-2007
I think you're looking for "return".

Code:
#!/bin/ksh
function workn { echo $1 && return $1; }
function brokn { echo $1 && exit $1; }
workn 1; echo "RC=$?"
brokn 2; echo "RC=$?"
workn 3; echo "RC=$?"
exit 0

RESULT:
Code:
1
RC=1
2

Note that the "workn" function returns and lets the script continue with the next line, but "brokn" exits immediately.

HOWEVER, it's generally a bad practice to put "exit"s or "return"s all over the place, since it starts to make the code much more non-linear and harder to read and debug. The best place to exit a function (or script, program, etc.) is at the bottom.
# 10  
Old 12-11-2007
shell functions should be terminated with "return", not with "exit".

"exit", as you have already noticed, will terminate the whole script. "return" will pass command back to the calling function. "return" accepts one integer parameter, which will be used as the errorlevel, which will be set for the calling function. See the example below:

Code:
#! /bin/ksh

subfunction ()
{
    typeset -i iRetVal=0   # experiment by setting this to other values

    print - "now in subfunction()"
    return $iRetVal
}

# main ()

print - "starting..."
subfunction
print - "subfunction returned $?"    # these values you will see here
print - "exiting...."
exit 0

I hope this helps.

bakunin
# 11  
Old 12-12-2007
Your tips worked fine

Quote:
Originally Posted by porter
If I have a script that looks like

Code:
usefull()
{
    exit
}

and run it with

Code:
./scriptname

nothing much will happen

if I run it with

Code:
. ./scriptname

nothing much will appear to happen except that the function will be remembered.

now if I type

Code:
usefull

bingo, the session dissappears.

No surprises, and everything doing what it says it will do.
Hi porter,
You are correct, I invoked the script by . ./somepat/scriptname. Now I copied the script in the home directory, from where I am executing this script.It's working fine.
# 12  
Old 12-12-2007
Quote:
Originally Posted by vino
It may depend on how you invoke the script.
If you invoke the script as . ./yourscript.sh, then it would exit from the session as well.
Oh well. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

ksh While Loop - passing variables to functions

I'm reading a text file using a while loop but when I call a function from within this loop it exits that same iteration … even though there are many more lines in the file to be read. I thought it was something to do with the IFS setting but it appears that a function call (when run... (3 Replies)
Discussion started by: user052009
3 Replies

2. Shell Programming and Scripting

Exit status of the ksh Script

Hi Im trying to write a script that will archive some file using java program.Below is the part of the script that I use and my problem is that the script always return with status 0.Below is part of my script(end part) purge.ksh echo "No of files before tar :... (4 Replies)
Discussion started by: saachinsiva
4 Replies

3. Shell Programming and Scripting

KSH: Confused with behaviour of exit

Hi Everyone, I am confused on why the below snippet of code is not working as I intend it to do. I have googled and confirmed that "exit" is supposed to abort the execution of the script regardless if the exit was called from inside a function, or the main body of the script. log_and_die() { ... (3 Replies)
Discussion started by: maddmaster
3 Replies

4. Shell Programming and Scripting

Functions, exit, and kill in bash

Hello Okay, for reasons related to sourcing a script from another script, I've had to put my main loop into a function, and from there I call other functions. My problem then is exiting from deep within the function call stack. I used to simply call exit, and that would accomplish what I... (1 Reply)
Discussion started by: brsett
1 Replies

5. Shell Programming and Scripting

Terminal is closing on exit in ksh

hi while executing the following script, my terminal window is getting closed if I enter a invalid option. I want the script should go back the the command prompt. how to do achive it. i execute the script as . ./test #! /usr/bin/ksh Printf " Type of Installer : \n\t\t 1. Whole Build... (3 Replies)
Discussion started by: vij_krr
3 Replies

6. Shell Programming and Scripting

Passing array to functions in ksh script

Let me know if there is a way to pass array to a funtion in ksh script. function isPresent { typeset member member=$1 dbList=$2 echo '$1:' $1 echo '$2' $dbList The array will be at the second position....something like this isPresent 12 <array> if then echo... (3 Replies)
Discussion started by: prasperl
3 Replies

7. Shell Programming and Scripting

KSH: Test telnet and exit

Hi, I need to do a test Telnet in KSH and if the connection is good then disconnect the telnet session with out logging in and without exiting the shell script. Example output of a good connection: $telnet xxx.xx.xx.xxx xxxx Trying xxx.xx.xx.xxx... Connected to xxx.xx.xx.xxx. Escape... (1 Reply)
Discussion started by: calex
1 Replies

8. Shell Programming and Scripting

KSH Functions String return

Hy everybody I'm trying to write a function like function(){ final=$1 return $final } but I get following error: "return: bad non-numeric arg ''" what shall I do? Isn't it possible to return strings?:confused: thanks lucae (2 Replies)
Discussion started by: lucae
2 Replies

9. Shell Programming and Scripting

ksh functions

Hi All, just wanted to ask if functions created in a Korn shell script can be passed parameters and if so what is the syntax for creating a function with parameters? thanks Mani (3 Replies)
Discussion started by: scriptingmani
3 Replies

10. Programming

Exit Code in HP-UX KSH.

In one of my programs another process is called using the system command e.g. lv_error = system("myproc"); where lv_error is declared as an int. myproc would be returning 0 for success and 1 for failure. e.g. if (success) { return(0); }else{ return(1); } When the return code... (3 Replies)
Discussion started by: mbb
3 Replies
Login or Register to Ask a Question