How to get the return code from a Unix command in Cobol



The problem: I want to run a Unix command, program or script from Cobol and get its return
code. For example, I want to test whether /usr/tmp is a valid directory. On a shell
command line, I can say 'test -d /usr/tmp' (or ksh shortcut [ -d /usr/tmp ]) followed by
'echo $?'. The result will be 0 if valid, 1 if invalid. When I say the same in a Cobol
program ..

call 'system' using 'test -d /usr/tmp'
if return-code not = zero
display '/usr/tmp is not a valid directory'
set fatal-error to true
end-if

... the return-code is alswya zero.

The solution:

call 'system' using 'test -d /usr/tmp; exit $?'

System() forks a child process, execs it to the default shell (usually sh), feeds it your
command line and waits for it to terminate. It is functionally the same as saying:
sh -c 'test -d /usr/tmp'
The sh shell is supposed to exit by default with the last return code ($?), and system()
is supposed to do the same. There is a bug in one of the two causing that to not work, but
it does work when your command does an explicit exit.

This question came up here two years ago, without a conclusive answer. I opined the return
code should be passed back, Richard thought it was fundamentally impossible for a child
process to pass anything back to its parent. This solution shows I was right.

A further complication is that the system() surrogate supplied Micro Focus Server Express
4.1 running on HP-UX shifts the return code left 8 bits. Thus,
call 'system' using 'exit 123'
returns 31,488, which is 123*256. The 1 returned by the above directory test comes back as
256. At least it's not zero, which is all you care about.
.



Relevant Pages

  • Re: How to get the return code from a Unix command in Cobol
    ... the return-code is alswya zero. ... Systemforks a child process, execs it to the default shell, feeds it your ... The requirement was to compile a single program on each ...
    (comp.lang.cobol)
  • Re: How to get the return code from a Unix command in Cobol
    ... the return-code is alswya zero. ... Systemforks a child process, execs it to the default shell, feeds it your ... The sh shell is supposed to exit by default with the last return code, ...
    (comp.lang.cobol)
  • Re: How to get the return code from a Unix command in Cobol
    ... I want to test whether /usr/tmp is a valid directory. ... the return-code is alswya zero. ... Systemforks a child process, execs it to the default shell, feeds it your ...
    (comp.lang.cobol)
  • Why newbies dont RTFM...
    ... Even though I've used Linux before, I've never had to do any ... BASH BUILTIN COMMANDS ... last command exited within ... unless the shell is not exeâ ...
    (comp.os.linux.misc)
  • Re: Great SWT Program
    ... None of the nasty things that you have said or implied about me are at ... treat the file as input (manually invoking the command interpreter ... script, copy the line into that within the editor, exit, and invoke ... the shell script. ...
    (comp.lang.java.programmer)