Execing Processes in COBOL
Our example demonstrates how to execute external processes in COBOL, similar to the exec
function in Unix-like operating systems. In COBOL, we don’t have a direct equivalent to exec
, but we can use the CALL
statement to run external programs.
In this COBOL program:
We use the
CALL "SYSTEM"
statement to execute thels -a -l -h
command. This is similar to usingexec.LookPath("ls")
andsyscall.Exec
in the original example.The command and its arguments are combined into a single string (
WS-COMMAND
), unlike in the original example where they were separate.We capture the return code of the command execution in
WS-RETURN-CODE
.The output of the command is redirected to a file (
command-output.txt
), which we then read and display.We don’t have direct access to environment variables like in the original example (
os.Environ()
). The system call will use the current environment.
To run this program:
This will execute the ls -a -l -h
command and display its output.
Note that COBOL doesn’t offer a direct equivalent to Unix’s fork
function. The CALL
statement in COBOL is typically used for executing subprograms or system commands, which covers many use cases for process execution in COBOL applications.