Operating System - HP-UX
1822582 Members
3359 Online
109642 Solutions
New Discussion юеВ

return value for a korn shell function

 
Shabu Khan_1
Advisor

return value for a korn shell function

hello all,
how do I check for the return value of a function in the calling program ...

for eg.

compute()
{
if ....then
echo ....
return this value 1 to the main program
else
echo blah ...
return this value 2 to the main program
fi
}

Main program (calling program/function)
for .... do
if ... then
compute # function name
if my compute function returns value 1 then
echo ...
else
echo ...
fi
done

what is the syntax I would use to check the return value of a function ...
I've tried the return command (return ) .. doesn't seem to get it right ...

any help would be appreciated ...

Thanks,
Shabu
10 REPLIES 10
Con O'Kelly
Honored Contributor

Re: return value for a korn shell function

Hi

echo $?

This will give you return value of a completed command.

Cheers
Con
Sanjay Kumar Suri
Honored Contributor

Re: return value for a korn shell function

Check the following:

if [ $? -ne 0 ]
then
echo "Do this"
sh other_commands
else
echo "Do that"
sh other_commands
fi

sks
A rigid mind is very sure, but often wrong. A flexible mind is generally unsure, but often right.
Shabu Khan_1
Advisor

Re: return value for a korn shell function

Thanks for your response ...

well, in that case echo $? will always return '0' which is not I want ...

Take a closer look at my example ...

-Shabu
KapilRaj
Honored Contributor

Re: return value for a korn shell function

the function should exit with a value

i.e.

function kapil {
if [ "$1" = "kapilraj"]
echo "$1 is a good chap"
exit 0
else
echo "$1 is a bad chap"
exit 8
fi
}

now if u call `kapil kapilraj` the return code will be "0" which can be checked by "$?" `kapil kaps` return code will be "8"

Kaps
Nothing is impossible
Con O'Kelly
Honored Contributor

Re: return value for a korn shell function

OK can you not just assign the return value you want to a variable as in:

if...then
echo...
RETCODE="value1"
else
echo...
RETCODE="value2"
fi

MAIN

if [ $RETCODE = "value1" ]
then
echo..
else
echo...
fi

Cheers
Con
Sanjay Kumar Suri
Honored Contributor

Re: return value for a korn shell function

Check the following variations (not tested):

if [ $? -eq 1 ]
then
echo "1"
elif [ $? -eq 2 ]
then
echo "2
else
echo "0"
fi

case $? in
1 ) echo "1 ;;
2 ) echo "2" ;;
0 ) echo "0";;
esac

sks
A rigid mind is very sure, but often wrong. A flexible mind is generally unsure, but often right.
Shabu Khan_1
Advisor

Re: return value for a korn shell function

Thanks Con, Sanjay and Kapil for your responses ...

After some testing I went with Con's tip ...
this is what I did ...

compute()
{
if ....then
export RC="$(echo ${?})"
else
echo blah ...
export RC="$(echo ${?})"
fi
}

for ...
if [ ${RC} -eq "0" ]; then
echo ....
ssh stuff ...
fi
done

Thanks again ...

-Shabu
Jose Mosquera
Honored Contributor

Re: return value for a korn shell function

Hi,

Please it always uses immediately the assignment of the return code after the command that you want to evaluate!

I comment you this because I observe that the condition "else" of your compute()function always return the value zero that generates the correct execution of the command "echo blah..."

I suggest you to assign the return code to a variable immediately after the execution of the command, and later to evaluate the content of the variable. i.e:

your_command
RETURN_CODE=$?
if [ "$RETURN_CODE" = "0" ]
then
true_condition...
else
false_condition...
fi

Rgds.
curt larson_1
Honored Contributor

Re: return value for a korn shell function

you'll need to do something like this

compute()
{
if ....then
echo ....
#return this value 1 to the main program
return 1
else
echo blah ...
#return this value 2 to the main program
return 2
fi
}

Main program (calling program/function)
for .... do
if ... then
compute # function name
#if my compute function returns value 1 then
if (( $? == 1 )) ;then
echo ...
else
echo ...
fi
done
Muthukumar_5
Honored Contributor

Re: return value for a korn shell function

We can get the return type of any operation or return values with $?. If we are going to process the $? value more,then it is good to store in a paritular variable. If you are going to do multiple actions, then case statements are good and effective for this.

Example:
set -x

fun()
{
input=$1

if [[ $input = "start" ]]
then
return 1
elif [[ $input = "shutdown" ]]
then
return 2
elif [[ $input = "end" ]]
then
return 3
fi

# Normal return
return 0
}

# main script
fun $1
RC=$?
if [[ $RC -eq 0 ]]
then
echo " Action is not available"
exit 1
elif [[ $RC -eq 1 ]]
then
echo " Action is start"
exit 0
elif [[ $RC -eq 2 ]]
then
echo " Action is shutdown"
exit 0
elif [[ $RC -eq 3 ]]
then
echo " Action is end"
exit 0
fi

# script return
exit 0


Use case statements instead of if-elif-else on main loop as,
case $? in
0)
echo " Action is not available" ;;
1)
echo " Action is start" ;;
2)
echo " Action is shutdown" ;;
3)
echo " Action is end" ;;
esac

There is no need for RC variable in case statements.

We can use a common variable to set a value on the function as,

fun1()
{
RC=0
if
RC=1
elif
RC=2
elif
RC=3
fi

return 0
}
fun1
if [[ $? -ne 0 ]]; then
echo "fun1 execution failed"
else
echo "fun1 returns the type as $RC"
fi

RC must be initialised in the first line of funtion to know the action type.



Easy to suggest when don't know about the problem!