Linux Notes: GnuCOBOL on CentOS-7 (and higher)

  1. The information presented here is intended for educational use by qualified computer technologists.
  2. The information presented here is provided free of charge, as-is, with no warranty of any kind.
Edit: 2024-03-16

Introduction

Back in the days of mainframes and minicomputers, COBOL licenses were very expensive (eg. starting at tens of thousands of dollars per machine).

The neat thing about Linux and open source software is that a fairly good COBOL compiler could be downloaded then installed for zero dollars.

The following instructions describe nothing new and are only meant to pique your curiosity

Installing GnuCOBOL

caveat: yum is the package manager on CentOS-7 and RHEL-7. Starting with version 8, yum commands are translated into dnf commands

command notes
yum list \*-release\* display all available distros
yum install epel-release
install yum support for the epel distro (required for gnucobol)
yum list \*cobol\*
display all available packages containing the string "cobol"
yum install gnucobol
install gnucobol
which cobc where was the binary stored

online help: https://gnucobol.sourceforge.io/doc/gnucobol.html

Demo-1 (hello.cob)

Remember that COBOL remarks begin in column-7 while code begins in column-8
This rule comes from the age of punched cards
12345678901234567890123456789012345678901234567890123456789012345678901234

      *> file   : hello.cob
      *> notes  : Sample COBOL program (old school)
      *> history:
      *> 2023-02-13 NSR original effort
      *> edit   : Neil Rieck 
      *> build  : cobc -x hello.cob
      *> execute: ./hello
       IDENTIFICATION DIVISION.
       PROGRAM-ID. hello.
       PROCEDURE DIVISION.
       DISPLAY "Hello World!".
       STOP RUN.

$ cobc -x hello.cob
$ ./hello
Hello World!
$ cobc -help
[[[ help text is displayed ]]]

Demo-2 (hellonew.cob)

      *> file   : hellonew.cob
      *> notes  : Sample GnuCOBOL program
      *> edit   : Neil Rieck (2023-02-13) 
      *> build  : cobc -x -free hellonew.cob
      *> execute: ./hellonew
       IDENTIFICATION DIVISION.
       PROGRAM-ID. hellonew.
       PROCEDURE DIVISION.
       DISPLAY
           "Hello World!"
       END-DISPLAY.
       GOBACK.

Demo-3 (demo-isam-001.cob)

Notes:

      *> ==========================================================
      *> title: demo-isam-001.cob
      *> date : 2023-02-14
      *> edit : Neil Rieck
      *> build: cobc -Wall -free -x demo-isam-001.cob
      *> run  : ./demo-isam-001
      *> ==========================================================
       IDENTIFICATION DIVISION.
       PROGRAM-ID. TEST-PROGRAM.
       
       ENVIRONMENT DIVISION.
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
           SELECT OPTIONAL D-FILE ASSIGN TO "TESTDB.ISQ" ORGANIZATION IS INDEXED ACCESS MODE IS DYNAMIC RECORD KEY IS D-ID.
       
       DATA DIVISION.
       FILE SECTION.
       FD D-FILE.
       01 D-RECORD.
           03 D-ID PIC X(8).
           03 D-IV PIC 9(8) COMP.
           03 D-XV COMP-2.
           03 D-SLEN PIC 9(4) COMP.
           03 D-SV PIC X(50).
       WORKING-STORAGE SECTION.
       01 NREC PIC 9(4) COMP VALUE 1000.
       01 BLANK-VAL PIC X(8) VALUE "        ".
       01 EOF-FLAG PIC X.
       01 DONE-FLAG PIC X.
       01 WID PIC 9(4) DISPLAY.
       01 IV PIC 9(4) DISPLAY.
       01 XV PIC 9(4)V9(2) DISPLAY.
       01 SLEN PIC 9(2) DISPLAY.
       01 SV PIC X(50).
       01 I PIC 9(4) DISPLAY.
       01 N PIC 9(4) DISPLAY.
       01 N2 PIC 9(4) DISPLAY.
       01 STOP-ID PIC X(8).
       
       PROCEDURE DIVISION.
       MAIN-PARAGRAPH.
           OPEN I-O D-FILE
           MOVE BLANK-VAL TO D-ID
           START D-FILE KEY GREATER THAN D-ID
               INVALID KEY DISPLAY "Empty file"
           END-START
           PERFORM VARYING I FROM 1 BY 1 UNTIL I > NREC
               COMPUTE WID = 1000 + I
               STRING "Key#" DELIMITED BY SIZE WID DELIMITED BY SIZE INTO D-ID
               COMPUTE D-IV = I
               COMPUTE D-XV = I
               COMPUTE D-SLEN = 18
               STRING "This is value " DELIMITED BY SIZE I DELIMITED BY SIZE INTO D-SV
               WRITE D-RECORD
                   INVALID KEY DISPLAY "Error writing"
                   NOT INVALID KEY CONTINUE
               END-WRITE
           END-PERFORM
           COMPUTE WID = 1077
           STRING "Key#" DELIMITED BY SIZE WID DELIMITED BY SIZE INTO D-ID
           PERFORM DUMP-PARAGRAPH
           DELETE D-FILE
               INVALID KEY DISPLAY "Error deleting"
               NOT INVALID KEY CONTINUE
           END-DELETE
           PERFORM DUMP-PARAGRAPH
           COMPUTE WID = 1088
           STRING "Key#" DELIMITED BY SIZE WID DELIMITED BY SIZE INTO D-ID
           PERFORM DUMP-PARAGRAPH
           READ D-FILE
               INVALID KEY DISPLAY "Error reading"
               NOT INVALID KEY CONTINUE
           END-READ
           COMPUTE D-IV = D-IV + 1
           COMPUTE D-XV = D-XV + 0.1
           MOVE D-SLEN TO SLEN
           MOVE D-SV TO SV
           STRING SV(1:SLEN) DELIMITED BY SIZE " updated" DELIMITED BY SIZE INTO D-SV
           COMPUTE D-SLEN = D-SLEN + 8
           REWRITE D-RECORD
               INVALID KEY DISPLAY "Error updating"
               NOT INVALID KEY CONTINUE
           END-REWRITE
           PERFORM DUMP-PARAGRAPH
           MOVE BLANK-VAL TO D-ID
           START D-FILE KEY IS GREATER THAN D-ID
               INVALID KEY DISPLAY "Error rewinding"
               NOT INVALID KEY CONTINUE
           END-START
           MOVE 'N' TO EOF-FLAG
           MOVE 0 TO N
           PERFORM UNTIL EOF-FLAG = 'Y'
               READ D-FILE NEXT
                   AT END MOVE 'Y' TO EOF-FLAG
                   NOT AT END PERFORM CHECK-AND-COUNT-PARAGRAPH
               END-READ
           END-PERFORM
           DISPLAY N
           COMPUTE WID = 1075
           STRING "Key#" DELIMITED BY SIZE WID DELIMITED BY SIZE INTO D-ID
           START D-FILE KEY IS GREATER THAN OR EQUAL TO D-ID
               INVALID KEY DISPLAY "Error searching"
               NOT INVALID KEY CONTINUE
           END-START
           COMPUTE WID = 1085
           STRING "Key#" DELIMITED BY SIZE WID DELIMITED BY SIZE INTO STOP-ID
           MOVE 'N' TO EOF-FLAG
           MOVE 'N' TO DONE-FLAG
           MOVE 0 TO N2
           PERFORM UNTIL EOF-FLAG = 'Y' OR DONE-FLAG = 'Y'
               READ D-FILE NEXT
                   AT END MOVE 'Y' TO EOF-FLAG
                   NOT AT END PERFORM COUNT-PARAGRAPH
               END-READ
           END-PERFORM
           DISPLAY N2
           CLOSE D-FILE
           STOP RUN.
       DUMP-PARAGRAPH.
           DISPLAY "Key=" D-ID
           READ D-FILE
               INVALID KEY DISPLAY "Not found"
               NOT INVALID KEY PERFORM DUMP-DATA-PARAGRAPH
           END-READ.
       DUMP-DATA-PARAGRAPH.
           MOVE D-IV TO IV
           MOVE D-XV TO XV
           MOVE D-SLEN TO SLEN
           MOVE D-SV TO SV
           DISPLAY  "(" IV "," XV "," SV(1:SLEN) ")".
       CHECK-AND-COUNT-PARAGRAPH.
           MOVE D-IV TO IV
           MOVE D-XV TO XV
           MOVE D-SLEN TO SLEN
           MOVE D-SV TO SV
           IF IV < 1 OR NREC < IV THEN
               DISPLAY  "Unexpected value: (" IV "," XV "," SV(1:SLEN) ")"
           END-IF
           COMPUTE N = N + 1.
       COUNT-PARAGRAPH.
           IF D-ID GREATER THAN OR EQUAL TO STOP-ID THEN
               MOVE 'Y' TO DONE-FLAG
           ELSE
               COMPUTE N2 = N2 + 1
           END-IF.

Links


Back to Home
Neil Rieck
Waterloo, Ontario, Canada.