title: How a function should exit when it called with here-document from shell script?
body: <p>I have a COBOL program which should be run thru shell script and should accept the values from the here document. in the here document, i should call a function that should let control to be exit abnormally with exit code. </p>
  
  <p>I have tried as below but it is not working for me.</p>
  
  <p><strong><em>This is my COBOL program:</em></strong></p>
  
  <pre><code>   01  WW-ANS              PIC X value space. 
  
  IRS-200.
        display "ARE THE ABOVE ANSWERS CORRECT? Y/N/E".
        Accept ws-ans.
        display "entered value is " ws-ans "&lt;".
  
        IF WW-ANS  =  "E"  or "e"
           PERFORM STOP-RUN-CA.
  
        IF WW-ANS  NOT =  "Y" AND "N" AND "E"      
                      and "y" and "n" and "e"      
            PERFORM DISPLAY-01 THRU DISPLAY-01-EXIT
            GO  TO  IRS-200.
  
        IF WW-ANS  =  "Y" or "y"                   
            display "Program executed successfully"
            PERFORM STOP-RUN-CA. 
        ELSE                                       
            GO  TO  IRS-200.
  
  DISPLAY-01.                       
        DISPLAY "value is &gt;" WW-ANS "&lt;".
        DISPLAY "INVALID RESPONSE".
  </code></pre>
  
  <p><strong><em>This is my shell script:</em></strong></p>
  
  <pre><code>#!/bin/bash
  funexit ()
  {
  echo "calling funexit"
  exit 1
  }
  
  /caplus/pub/test123&lt;&lt;:EOD:
  1
  g
  $(funexit)
  Y
  :EOD:
  </code></pre>
  
  <p><strong><em>Output is:</em></strong></p>
  
  <pre><code>[Linux Dev:adminusr ~]$ ./test123.sh
  ARE THE ABOVE ANSWERS CORRECT? Y/N/E
  entered value is 1&lt;
  value is &gt;1&lt;
  INVALID RESPONSE
  ARE THE ABOVE ANSWERS CORRECT? Y/N/E
  entered value is g&lt;
  value is &gt;G&lt;
  INVALID RESPONSE
  ARE THE ABOVE ANSWERS CORRECT? Y/N/E
  entered value is  c&lt;
  value is &gt;C&lt;
  INVALID RESPONSE
  ARE THE ABOVE ANSWERS CORRECT? Y/N/E
  entered value is Y&lt;
  Program executed successfully
  </code></pre>
  
  <p>When ever function gets called from here document the COBOL program accept the value as "C", since at function: it invoke the echo command and considering the first character from the "calling funexit" string, instead of getting exit.</p>
  
  <p><strong><em>From the function, I have removed echo statement like below:</em></strong></p>
  
  <pre><code>#!/bin/bash
  funexit ()
  {
  exit 1
  }
  
  /caplus/pub/test123&lt;&lt;:EOD:
  1
  g
  $(funexit)
  Y
  :EOD:
  </code></pre>
  
  <p><strong><em>Output is:</em></strong></p>
  
  <pre><code>[Linux Dev:adminusr ~]$ ./test123.sh
  ARE THE ABOVE ANSWERS CORRECT? Y/N/E
  entered value is 1&lt;
  value is &gt;1&lt;
  INVALID RESPONSE
  ARE THE ABOVE ANSWERS CORRECT? Y/N/E
  entered value is g&lt;
  value is &gt;G&lt;
  INVALID RESPONSE
  ARE THE ABOVE ANSWERS CORRECT? Y/N/E
  entered value is   &lt;
  value is &gt; &lt;
  INVALID RESPONSE
  ARE THE ABOVE ANSWERS CORRECT? Y/N/E
  entered value is Y&lt;
  Program executed successfully.
  </code></pre>
  
  <p>When ever function gets called from here document the COBOL program accept the value as spaces instead of getting exit.</p>
  
  <p>The script should get exit abnormally with some exit code.</p>