2 novembre 2017

Executer une commande système dans un traitement Cobol

Le site MicroFocus donne un exemple de Cobol exécutant une commande système et récupérant le code retour :
https://community.microfocus.com/microfocus/cobol/net_express__server_express/w/knowledge_base/6537/invoke-a-unix-command-from-within-cobol-using-call-system-and-retrieve-the-return-code

Dans le principe, on utilise la commande Cobol
CALL "SYSTEM" USING null-terminated-command  RETURNING return-code-ws.

La commande est terminée par un caractère NULL :

000014 01 null-terminated-command.
000015     05 command      pic x(2048).
000016     05 filler          pic x value x"00".


Le code retour est placé dans un numérique :
000002 01 return-code-ws     pic s9(8) comp-5.
L'article explique que pour AIX, Solaris, HP/UX PARISC and HP/UX Itanium (big-endian) il faut la redéfinir ainsi :
000005     05 filler          pic x.
000006     05 high-order-byte pic s9 comp-5.
000007     05 filler          pic x.
000008     05 low-order-byte pic s9 comp-5.

Et pour Linux x86, HP/Tru64, SCO, UnixWare - et certainement Windows - (little-endian) ainsi :
000010     05 filler          pic xx.
000011     05 high-order-byte pic s9 comp-5.
000012     05 low-order-byte pic s9 comp-5.


Pour le contrôler :
000024      if low-order-byte = 0
000025          display "return code from shell is: ",
000026               high-order-byte
000027      end-if


J'ai testé l'exemple fourni sur un AIX (donc un "big-endian") avec succès :

cob -vx callsys.cbl -o callsys
 

callsys

Enter command to be executed by shell
 (enter 'done' to quit)
ls -l  /tmp/dummy
-rw-r--r--    1 hraref   hraref          0 Aug 28 11:58 /tmp/dummy
return code from shell is: +000

Enter command to be executed by shell
 (enter 'done' to quit)
rm /tmp/dummy
rm: /tmp/dummy not removed.
Not owner
return code from shell is: +002


Merci à l'auteur de l'article ...

1 commentaire:

  1. A associer par exemple à un traitement en TBP079 (pour assurer l'intégrité de la transaction) ...

    RépondreSupprimer