- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- return value for a korn shell function
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-19-2004 06:43 PM
тАО07-19-2004 06:43 PM
return value for a korn shell function
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
any help would be appreciated ...
Thanks,
Shabu
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-19-2004 06:45 PM
тАО07-19-2004 06:45 PM
Re: return value for a korn shell function
echo $?
This will give you return value of a completed command.
Cheers
Con
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-19-2004 06:48 PM
тАО07-19-2004 06:48 PM
Re: return value for a korn shell function
if [ $? -ne 0 ]
then
echo "Do this"
sh other_commands
else
echo "Do that"
sh other_commands
fi
sks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-19-2004 06:50 PM
тАО07-19-2004 06:50 PM
Re: return value for a korn shell function
well, in that case echo $? will always return '0' which is not I want ...
Take a closer look at my example ...
-Shabu
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-19-2004 06:52 PM
тАО07-19-2004 06:52 PM
Re: return value for a korn shell function
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-19-2004 06:56 PM
тАО07-19-2004 06:56 PM
Re: return value for a korn shell function
if...then
echo...
RETCODE="value1"
else
echo...
RETCODE="value2"
fi
MAIN
if [ $RETCODE = "value1" ]
then
echo..
else
echo...
fi
Cheers
Con
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-19-2004 06:57 PM
тАО07-19-2004 06:57 PM
Re: return value for a korn shell function
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-19-2004 07:13 PM
тАО07-19-2004 07:13 PM
Re: return value for a korn shell function
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-19-2004 07:49 PM
тАО07-19-2004 07:49 PM
Re: return value for a korn shell function
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-19-2004 08:26 PM
тАО07-19-2004 08:26 PM
Re: return value for a korn shell function
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-19-2004 08:54 PM
тАО07-19-2004 08:54 PM
Re: return value for a korn shell function
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.