How to exit the KSH functions

Hi I am having the script which contains more functions. I want to exit the function if any failure. I tried with exit - the session itself is getting logged out. How can i fix this issue?

It would help if you explained how you were using the script and it's functions.

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.

Hi
function imple()
{
if [ ! -d $sourcedir ] ; then
echo " $sourcedir is not found from the release directory $RELPATH " | tee -a $REPLIMPLOG
exit // This exit is throwing me out of the session
elif [ ! -d $destdir ] ; then
echo " $destdir is not available in the production environment. " | tee -a $REPLIMPLOG
exit // This exit is throwing me out of the session
elif [ ! -d $destbak ] ; then
echo " $destbak is not available in the production environment. " | tee -a $RELIMPLOG
exit // This exit is throwing me out of the session
else
valid_flag=0
fi
}

I hope this is enough for u.

That is what exit does, it terminates the current process.

How are you invoking this script?

This function is from the script only. The big script is having multiple functions. This function is one among that. This is not seperate script. (This is function)

Yes, but as porter asked, how are you invoking the big script? Are you running it as: /path/to/script or 'cd /path/to/script; ./script' or what?

If I have a script that looks like

usefull()
{
    exit
}

and run it with

./scriptname

nothing much will happen

if I run it with

. ./scriptname

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

now if I type

usefull

bingo, the session dissappears.

No surprises, and everything doing what it says it will do.

I think you're looking for "return".

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

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.

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:

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

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.

Oh well. :frowning: