Skip to main content

Setting the UNIX $? to a Return value from COBOL

  • January 11, 2018
  • 0 replies
  • 0 views

Problem: 

What is the best way to return success or failure return codes from a COBOL program to UNIX? We want the "$?" in UNIX to contain the return code. How can we do that?

As background: The UNIX "$?" is set to 0 if the process just ended, ended successfully, and is set to a non-zero value if the process just ended does not end successfully (for example, if you try to cat a non-existent file you would get a non-zero result in $?):

 

[nwb-sol10v2-hub]cat ERPROBEN.cbl; echo $?
cat: cannot open ERPROBEN.cbl
2

 

Here is the illustration of the problem. A very simple COBOL test program contains the following:

 

       WORKING-STORAGE SECTION.
       01 MYVAR     PIC X(01).

       PROCEDURE DIVISION.
       1000-MAIN.

           MOVE 1 to MYVAR.

      *    MOVE 0 to MYVAR.

 

           IF MYVAR=1

               EXIT PROGRAM RETURNING 1

           ELSE

              EXIT PROGRAM RETURNING 0.

           IF MYVAR=1

              GOBACK RETURNING 1

           ELSE

               GOBACK RETURNING 0.

 

           EXIT PROGRAM.

           STOP RUN.

 

In this case we want the "$?" in UNIX to contain the value returned by EXIT PROGRAM and GOBACK RETURNING.

Yet -

EXIT PROGRAM RETURNING 1 does not 1 to Unix $?

While, GOBACK RETURNING 1 does return 1 to Unix $? 

Why is this happening and how do we get $? set to the return code?

 

Resolution:

A GOBACK and an EXIT PROGRAM have different behavior.  EXIT PROGRAM RETURNING 1 does not pass a return-code back to the O/S, GOBACK RETURNING 1 does.

Thee default behavior can be changed using the EXIT-PROGRAM compiler directive. By specifying the directive:


                exit-program(goback)


the program will now actually stop at the EXIT PROGRAM statement, and the 1 specified on the EXIT PROGRAM will be returned to the O/S.


#MFDS
#EnterpriseDeveloper

0 replies

Be the first to reply!