<?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 Korn Shell question in Operating System - HP-UX</title>
    <link>https://community.hpe.com/t5/operating-system-hp-ux/korn-shell-question/m-p/4790256#M641219</link>
    <description>Hi,&lt;BR /&gt;&lt;BR /&gt;how do I exit a script from a function that is called as var=$(&lt;MY function=""&gt;)?&lt;BR /&gt;The following example always prints "x: 1" whereas I would expect it to exit in the test1 function and thus only print "1" &lt;BR /&gt;&lt;BR /&gt;"&lt;BR /&gt;#!/usr/bin/ksh&lt;BR /&gt;&lt;BR /&gt;function test1&lt;BR /&gt;{&lt;BR /&gt;  echo "1"&lt;BR /&gt;  exit 1&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;x=$(test1)&lt;BR /&gt;echo "x: $x"&lt;BR /&gt;"&lt;BR /&gt;I tested this on Linux with zsh running as ksh and on HP-UX 11.11.&lt;BR /&gt;&lt;BR /&gt;Any help much appreciated.&lt;BR /&gt;&lt;BR /&gt;Kris&lt;/MY&gt;</description>
    <pubDate>Fri, 20 May 2011 08:14:51 GMT</pubDate>
    <dc:creator>Quark</dc:creator>
    <dc:date>2011-05-20T08:14:51Z</dc:date>
    <item>
      <title>Korn Shell question</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/korn-shell-question/m-p/4790256#M641219</link>
      <description>Hi,&lt;BR /&gt;&lt;BR /&gt;how do I exit a script from a function that is called as var=$(&lt;MY function=""&gt;)?&lt;BR /&gt;The following example always prints "x: 1" whereas I would expect it to exit in the test1 function and thus only print "1" &lt;BR /&gt;&lt;BR /&gt;"&lt;BR /&gt;#!/usr/bin/ksh&lt;BR /&gt;&lt;BR /&gt;function test1&lt;BR /&gt;{&lt;BR /&gt;  echo "1"&lt;BR /&gt;  exit 1&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;x=$(test1)&lt;BR /&gt;echo "x: $x"&lt;BR /&gt;"&lt;BR /&gt;I tested this on Linux with zsh running as ksh and on HP-UX 11.11.&lt;BR /&gt;&lt;BR /&gt;Any help much appreciated.&lt;BR /&gt;&lt;BR /&gt;Kris&lt;/MY&gt;</description>
      <pubDate>Fri, 20 May 2011 08:14:51 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/korn-shell-question/m-p/4790256#M641219</guid>
      <dc:creator>Quark</dc:creator>
      <dc:date>2011-05-20T08:14:51Z</dc:date>
    </item>
    <item>
      <title>Re: Korn Shell question</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/korn-shell-question/m-p/4790257#M641220</link>
      <description>&lt;P&gt;Kris,&lt;BR /&gt;&lt;BR /&gt;Well it would never just print "1" as the command substitution you are using is assigning that value to x rather than echoing it to the terminal.&lt;BR /&gt;&lt;BR /&gt;from the man page for ksh :&lt;BR /&gt;&lt;BR /&gt;Command substitution of most special commands (built-ins) that do not&lt;BR /&gt;perform I/O redirection are carried out without creating a separate&lt;BR /&gt;process. However, command substitution of a function creates a&lt;BR /&gt;separate process to run the function and all commands (built-in or&lt;BR /&gt;otherwise) in that function.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;So when you do command substitution - $()of a function that runs in a seperate process, so the exit call just exits that other process and returns to the main parent shell script process.&lt;BR /&gt;&lt;BR /&gt;Generally I use "return" in functions rather than "exit" and then test the return value from the function to decide what to do...&lt;BR /&gt;&lt;BR /&gt;Obviously this is a "constructed" test - what are you actually trying to acheive here?&lt;BR /&gt;&lt;BR /&gt;HTH&lt;BR /&gt;&lt;BR /&gt;Duncan&lt;/P&gt;</description>
      <pubDate>Tue, 29 Jun 2021 11:48:26 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/korn-shell-question/m-p/4790257#M641220</guid>
      <dc:creator>Duncan Edmonstone</dc:creator>
      <dc:date>2021-06-29T11:48:26Z</dc:date>
    </item>
    <item>
      <title>Re: Korn Shell question</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/korn-shell-question/m-p/4790258#M641221</link>
      <description>Thanks Duncan, you confirmed what I was suspecting.&lt;BR /&gt;&lt;BR /&gt;I just ran into this problem when I was reviewing code from somebody else where the script just continued after an exit 1 in a function that was used in cmd substitution.&lt;BR /&gt;&lt;BR /&gt;I will need to change the logic of the script a bit and like you suggested test for the return code.&lt;BR /&gt;&lt;BR /&gt;Thanks for your help</description>
      <pubDate>Fri, 20 May 2011 10:11:19 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/korn-shell-question/m-p/4790258#M641221</guid>
      <dc:creator>Quark</dc:creator>
      <dc:date>2011-05-20T10:11:19Z</dc:date>
    </item>
  </channel>
</rss>

