<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: return value for a korn shell function in Operating System - HP-UX</title>
    <link>https://community.hpe.com/t5/operating-system-hp-ux/return-value-for-a-korn-shell-function/m-p/3335919#M874243</link>
    <description>Hi&lt;BR /&gt;&lt;BR /&gt;echo $?&lt;BR /&gt;&lt;BR /&gt;This will give you return value of a completed command.&lt;BR /&gt;&lt;BR /&gt;Cheers&lt;BR /&gt;Con</description>
    <pubDate>Tue, 20 Jul 2004 01:45:41 GMT</pubDate>
    <dc:creator>Con O'Kelly</dc:creator>
    <dc:date>2004-07-20T01:45:41Z</dc:date>
    <item>
      <title>return value for a korn shell function</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/return-value-for-a-korn-shell-function/m-p/3335918#M874242</link>
      <description>hello all,&lt;BR /&gt;how do I check for the return value of a function in the calling program ...&lt;BR /&gt;&lt;BR /&gt;for eg.&lt;BR /&gt;&lt;BR /&gt;compute()&lt;BR /&gt;{&lt;BR /&gt;if ....then&lt;BR /&gt;  echo ....&lt;BR /&gt;  return this value 1 to the main program&lt;BR /&gt;else&lt;BR /&gt;  echo blah ...&lt;BR /&gt;  return this value 2 to the main program&lt;BR /&gt;fi&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;Main program (calling program/function)&lt;BR /&gt;for .... do&lt;BR /&gt; if ... then&lt;BR /&gt;   compute # function name&lt;BR /&gt;   if my compute function returns value 1 then&lt;BR /&gt;     echo ...&lt;BR /&gt;   else&lt;BR /&gt;     echo ...&lt;BR /&gt;   fi&lt;BR /&gt;done&lt;BR /&gt;&lt;BR /&gt;what is the syntax I would use to check the return value of a function ... &lt;BR /&gt;I've tried the return command (return &lt;VALUE&gt;) .. doesn't seem to get it right ...&lt;BR /&gt;&lt;BR /&gt;any help would be appreciated ...&lt;BR /&gt;&lt;BR /&gt;Thanks,&lt;BR /&gt;Shabu&lt;BR /&gt;&lt;/VALUE&gt;</description>
      <pubDate>Tue, 20 Jul 2004 01:43:40 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/return-value-for-a-korn-shell-function/m-p/3335918#M874242</guid>
      <dc:creator>Shabu Khan_1</dc:creator>
      <dc:date>2004-07-20T01:43:40Z</dc:date>
    </item>
    <item>
      <title>Re: return value for a korn shell function</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/return-value-for-a-korn-shell-function/m-p/3335919#M874243</link>
      <description>Hi&lt;BR /&gt;&lt;BR /&gt;echo $?&lt;BR /&gt;&lt;BR /&gt;This will give you return value of a completed command.&lt;BR /&gt;&lt;BR /&gt;Cheers&lt;BR /&gt;Con</description>
      <pubDate>Tue, 20 Jul 2004 01:45:41 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/return-value-for-a-korn-shell-function/m-p/3335919#M874243</guid>
      <dc:creator>Con O'Kelly</dc:creator>
      <dc:date>2004-07-20T01:45:41Z</dc:date>
    </item>
    <item>
      <title>Re: return value for a korn shell function</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/return-value-for-a-korn-shell-function/m-p/3335920#M874244</link>
      <description>Check the following:&lt;BR /&gt;&lt;BR /&gt;if [ $? -ne 0 ]&lt;BR /&gt;then&lt;BR /&gt;  echo "Do this"&lt;BR /&gt;  sh other_commands&lt;BR /&gt;else&lt;BR /&gt;  echo "Do that"&lt;BR /&gt;  sh other_commands&lt;BR /&gt;fi&lt;BR /&gt;&lt;BR /&gt;sks&lt;BR /&gt;</description>
      <pubDate>Tue, 20 Jul 2004 01:48:26 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/return-value-for-a-korn-shell-function/m-p/3335920#M874244</guid>
      <dc:creator>Sanjay Kumar Suri</dc:creator>
      <dc:date>2004-07-20T01:48:26Z</dc:date>
    </item>
    <item>
      <title>Re: return value for a korn shell function</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/return-value-for-a-korn-shell-function/m-p/3335921#M874245</link>
      <description>Thanks for your response ...&lt;BR /&gt;&lt;BR /&gt;well, in that case echo $? will always return '0' which is not I want ...&lt;BR /&gt;&lt;BR /&gt;Take a closer look at my example ...&lt;BR /&gt;&lt;BR /&gt;-Shabu</description>
      <pubDate>Tue, 20 Jul 2004 01:50:04 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/return-value-for-a-korn-shell-function/m-p/3335921#M874245</guid>
      <dc:creator>Shabu Khan_1</dc:creator>
      <dc:date>2004-07-20T01:50:04Z</dc:date>
    </item>
    <item>
      <title>Re: return value for a korn shell function</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/return-value-for-a-korn-shell-function/m-p/3335922#M874246</link>
      <description>the function should exit with a value&lt;BR /&gt;&lt;BR /&gt;i.e.&lt;BR /&gt;&lt;BR /&gt;function kapil {&lt;BR /&gt;if [ "$1" = "kapilraj"]&lt;BR /&gt;echo "$1 is a good chap"&lt;BR /&gt;exit 0&lt;BR /&gt;else&lt;BR /&gt;echo "$1 is a bad chap"&lt;BR /&gt;exit 8&lt;BR /&gt;fi&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;now if u call `kapil kapilraj` the return code will be "0" which can be checked by "$?" `kapil kaps` return code will be "8"&lt;BR /&gt;&lt;BR /&gt;Kaps</description>
      <pubDate>Tue, 20 Jul 2004 01:52:04 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/return-value-for-a-korn-shell-function/m-p/3335922#M874246</guid>
      <dc:creator>KapilRaj</dc:creator>
      <dc:date>2004-07-20T01:52:04Z</dc:date>
    </item>
    <item>
      <title>Re: return value for a korn shell function</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/return-value-for-a-korn-shell-function/m-p/3335923#M874247</link>
      <description>OK can you not just assign the return value you want to a variable as in:&lt;BR /&gt;&lt;BR /&gt;if...then&lt;BR /&gt;echo...&lt;BR /&gt;RETCODE="value1"&lt;BR /&gt;else&lt;BR /&gt;echo...&lt;BR /&gt;RETCODE="value2"&lt;BR /&gt;fi&lt;BR /&gt;&lt;BR /&gt;MAIN&lt;BR /&gt;&lt;BR /&gt;if [ $RETCODE = "value1" ]&lt;BR /&gt;then&lt;BR /&gt;echo..&lt;BR /&gt;else &lt;BR /&gt;echo...&lt;BR /&gt;fi&lt;BR /&gt;&lt;BR /&gt;Cheers&lt;BR /&gt;Con&lt;BR /&gt;</description>
      <pubDate>Tue, 20 Jul 2004 01:56:23 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/return-value-for-a-korn-shell-function/m-p/3335923#M874247</guid>
      <dc:creator>Con O'Kelly</dc:creator>
      <dc:date>2004-07-20T01:56:23Z</dc:date>
    </item>
    <item>
      <title>Re: return value for a korn shell function</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/return-value-for-a-korn-shell-function/m-p/3335924#M874248</link>
      <description>Check the following variations (not tested):&lt;BR /&gt;&lt;BR /&gt;if [  $? -eq 1 ]&lt;BR /&gt;then&lt;BR /&gt;echo "1"  &lt;BR /&gt;elif [  $? -eq 2 ]&lt;BR /&gt;then&lt;BR /&gt;echo "2  &lt;BR /&gt;else&lt;BR /&gt;echo "0"  &lt;BR /&gt;fi&lt;BR /&gt;&lt;BR /&gt;case $? in&lt;BR /&gt;1 ) echo "1 ;;&lt;BR /&gt;2 ) echo "2" ;;&lt;BR /&gt;0 ) echo "0";;&lt;BR /&gt;esac&lt;BR /&gt;&lt;BR /&gt;sks</description>
      <pubDate>Tue, 20 Jul 2004 01:57:43 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/return-value-for-a-korn-shell-function/m-p/3335924#M874248</guid>
      <dc:creator>Sanjay Kumar Suri</dc:creator>
      <dc:date>2004-07-20T01:57:43Z</dc:date>
    </item>
    <item>
      <title>Re: return value for a korn shell function</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/return-value-for-a-korn-shell-function/m-p/3335925#M874249</link>
      <description>Thanks Con, Sanjay and Kapil for your responses ...&lt;BR /&gt;&lt;BR /&gt;After some testing I went with Con's tip ...&lt;BR /&gt;this is what I did ...&lt;BR /&gt;&lt;BR /&gt;compute()&lt;BR /&gt;{&lt;BR /&gt;if ....then&lt;BR /&gt; export RC="$(echo ${?})"&lt;BR /&gt;else&lt;BR /&gt; echo blah ...&lt;BR /&gt; export RC="$(echo ${?})"&lt;BR /&gt;fi&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;for ...&lt;BR /&gt; if [ ${RC} -eq "0" ]; then&lt;BR /&gt;   echo ....&lt;BR /&gt;   ssh stuff ...&lt;BR /&gt; fi&lt;BR /&gt;done&lt;BR /&gt;&lt;BR /&gt;Thanks again ...&lt;BR /&gt;&lt;BR /&gt;-Shabu</description>
      <pubDate>Tue, 20 Jul 2004 02:13:12 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/return-value-for-a-korn-shell-function/m-p/3335925#M874249</guid>
      <dc:creator>Shabu Khan_1</dc:creator>
      <dc:date>2004-07-20T02:13:12Z</dc:date>
    </item>
    <item>
      <title>Re: return value for a korn shell function</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/return-value-for-a-korn-shell-function/m-p/3335926#M874250</link>
      <description>Hi,&lt;BR /&gt;&lt;BR /&gt;Please it always uses immediately the assignment of the return code after the command that you want to evaluate!&lt;BR /&gt;&lt;BR /&gt;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..."&lt;BR /&gt;&lt;BR /&gt;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:&lt;BR /&gt;&lt;BR /&gt;your_command&lt;BR /&gt;RETURN_CODE=$?&lt;BR /&gt;if [ "$RETURN_CODE" = "0" ]&lt;BR /&gt;then&lt;BR /&gt;   true_condition...&lt;BR /&gt;else&lt;BR /&gt;   false_condition...&lt;BR /&gt;fi&lt;BR /&gt;&lt;BR /&gt;Rgds.</description>
      <pubDate>Tue, 20 Jul 2004 02:49:32 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/return-value-for-a-korn-shell-function/m-p/3335926#M874250</guid>
      <dc:creator>Jose Mosquera</dc:creator>
      <dc:date>2004-07-20T02:49:32Z</dc:date>
    </item>
    <item>
      <title>Re: return value for a korn shell function</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/return-value-for-a-korn-shell-function/m-p/3335927#M874251</link>
      <description>you'll need to do something like this&lt;BR /&gt;&lt;BR /&gt;compute()&lt;BR /&gt;{&lt;BR /&gt;if ....then&lt;BR /&gt;echo ....&lt;BR /&gt;#return this value 1 to the main program&lt;BR /&gt;return 1&lt;BR /&gt;else&lt;BR /&gt;echo blah ...&lt;BR /&gt;#return this value 2 to the main program&lt;BR /&gt;return 2&lt;BR /&gt;fi&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;Main program (calling program/function)&lt;BR /&gt;for .... do&lt;BR /&gt;if ... then&lt;BR /&gt;compute # function name&lt;BR /&gt;#if my compute function returns value 1 then&lt;BR /&gt;if (( $? == 1 )) ;then&lt;BR /&gt;echo ...&lt;BR /&gt;else&lt;BR /&gt;echo ...&lt;BR /&gt;fi&lt;BR /&gt;done&lt;BR /&gt;</description>
      <pubDate>Tue, 20 Jul 2004 03:26:54 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/return-value-for-a-korn-shell-function/m-p/3335927#M874251</guid>
      <dc:creator>curt larson_1</dc:creator>
      <dc:date>2004-07-20T03:26:54Z</dc:date>
    </item>
    <item>
      <title>Re: return value for a korn shell function</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/return-value-for-a-korn-shell-function/m-p/3335928#M874252</link>
      <description>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.&lt;BR /&gt;&lt;BR /&gt;Example:&lt;BR /&gt;set -x&lt;BR /&gt;&lt;BR /&gt;fun()&lt;BR /&gt;{&lt;BR /&gt;        input=$1&lt;BR /&gt;&lt;BR /&gt;        if [[ $input = "start" ]]&lt;BR /&gt;        then&lt;BR /&gt;                return 1&lt;BR /&gt;        elif [[ $input = "shutdown" ]]&lt;BR /&gt;        then&lt;BR /&gt;                return 2&lt;BR /&gt;        elif [[ $input = "end" ]]&lt;BR /&gt;        then&lt;BR /&gt;                return 3&lt;BR /&gt;        fi&lt;BR /&gt;&lt;BR /&gt;        # Normal return&lt;BR /&gt;        return 0&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;# main script&lt;BR /&gt;fun $1&lt;BR /&gt;RC=$?&lt;BR /&gt;if [[ $RC -eq 0 ]]&lt;BR /&gt;then&lt;BR /&gt;        echo " Action is not available"&lt;BR /&gt;        exit 1&lt;BR /&gt;elif [[ $RC -eq 1 ]]&lt;BR /&gt;then&lt;BR /&gt;        echo " Action is start"&lt;BR /&gt;        exit 0&lt;BR /&gt;elif [[ $RC -eq 2 ]]&lt;BR /&gt;then&lt;BR /&gt;        echo " Action is shutdown"&lt;BR /&gt;        exit 0&lt;BR /&gt;elif [[ $RC -eq 3 ]]&lt;BR /&gt;then&lt;BR /&gt;        echo " Action is end"&lt;BR /&gt;        exit 0&lt;BR /&gt;fi&lt;BR /&gt;&lt;BR /&gt;# script return&lt;BR /&gt;exit 0&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Use case statements instead of if-elif-else on main loop as,&lt;BR /&gt;case $? in&lt;BR /&gt;0)&lt;BR /&gt;        echo " Action is not available" ;;&lt;BR /&gt;1)&lt;BR /&gt;        echo " Action is start" ;;&lt;BR /&gt;2)&lt;BR /&gt;        echo " Action is shutdown" ;;&lt;BR /&gt;3)&lt;BR /&gt;        echo " Action is end" ;;&lt;BR /&gt;esac&lt;BR /&gt;&lt;BR /&gt;There is no need for RC variable in case statements.&lt;BR /&gt;&lt;BR /&gt; We can use a common variable to set a value on the function as,&lt;BR /&gt;&lt;BR /&gt; fun1()&lt;BR /&gt; {&lt;BR /&gt;   RC=0&lt;BR /&gt;    if &lt;CHECK&gt;&lt;BR /&gt;      RC=1&lt;BR /&gt;    elif &lt;CHECK&gt;&lt;BR /&gt;      RC=2&lt;BR /&gt;    elif &lt;CHECK&gt;&lt;BR /&gt;      RC=3&lt;BR /&gt;    fi&lt;BR /&gt;   &lt;BR /&gt;   return 0&lt;BR /&gt;  }&lt;BR /&gt;  fun1 &lt;CHECK-TYPE&gt;&lt;BR /&gt;  if [[ $? -ne 0 ]]; then&lt;BR /&gt;    echo "fun1 execution failed"&lt;BR /&gt;  else&lt;BR /&gt;    echo "fun1 returns the type as $RC"&lt;BR /&gt;  fi&lt;BR /&gt;&lt;BR /&gt; RC must be initialised in the first line of funtion to know the action type.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;  &lt;BR /&gt;&lt;/CHECK-TYPE&gt;&lt;/CHECK&gt;&lt;/CHECK&gt;&lt;/CHECK&gt;</description>
      <pubDate>Tue, 20 Jul 2004 03:54:24 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/return-value-for-a-korn-shell-function/m-p/3335928#M874252</guid>
      <dc:creator>Muthukumar_5</dc:creator>
      <dc:date>2004-07-20T03:54:24Z</dc:date>
    </item>
  </channel>
</rss>

