Thursday, May 21, 2009

AS400 API - QCMDEXC - Execute Command

The Execute Command (QCMDEXC) program is an ibm as400 API that runs a single command.

The command runs as if it was not in a program. Therefore, variables cannot be used on the command because values cannot be returned by the as400 command to CL variables. Additionally, commands that can only be used in CL procedures or programs cannot be run by the QCMDEXC program. The format of the call to the QCMDEXC program is the following:

CALL PGM(QCMDEXC) PARM(command command-length)

Enter the command you want to run as a character string on the first parameter. You must specify the command library.

CALL PGM(QCMDEXC ) PARM('QSYS/CRTLIB LIB(TEST)' 22)

Remember that you must enclose the as400 command in single quotation marks if it contains blanks. The maximum length of the character string is 6000 characters; never count the delimiters (the single quotation marks ) as part of the string. The length that is specified as the second value on the PARM parameter is the length of the character string that is passed as the command. Length must be a packed decimal value of length 15 with 5 decimal positions

Example of Using QCMDEXC in an RPGLE Procedure

d Quote           C                   Const(X'7D')   
d StdCmd          S            200A                 
d StdLen          S             15P 5               

D Execute         PR                  ExtPgm('QCMDEXC')             
D     CmdStr                          Like(StdCmd)                 
D                                     Options(*VarSize)             
D                                     Const                         
D     CmdStrLen                       Like(StdLen) Const           
D*    IGCPrcCtl                  3    Options( *NoPass )           
D*                                    Const                         
*--------------------------------------------------               
d var1            S                   Like(StdCmd)                 
d var2            S                   Like(stdLen)                 
d                                     Inz(%size(var1))             

c                   eval      var1 =                               
c                             'CPYTOSTMF' + ' ' +                   
c                             'FROMMBR(' + Quote +                 
c                             '/QSYS.LIB/LIBNAME.LIB' +           
c                             '/QRPGLESRC.FILE/RPG1.MBR' +   
c                             Quote + ') ' +                       
c                             'TOSTMF(' + Quote +                   
c                             '/home/test.txt' +       
c                             Quote + ') ' +                       
c                             'STMFOPT(*REPLACE)'                 

c                   callp     Execute(var1:var2)                   

c                   eval      *inlr = *on

Another example of using QCMDEXC to execute CL commands

*                                                         
d CmdLength       s             15  5                      
d CmdString       s            256                         
 *                                                         
 * Procedure calls                                         
 *                                                         
d Command         pr                  EXTPGM('QCMDEXC')    
d  CmdString                   256                         
d  CmdLength                    15  5                      
 *                                                         
c                   eval      *inlr = *on                  
   //=========================================             
   //  *inzsr - One time run subroutine.                   
   //=========================================             
                                                           
 /free                                                     
   begsr *inzsr;
                                           
   cmdstring = 'dlyjob 10';                                
   cmdlength = %len(%trim(cmdstring));                     
   command(cmdstring : cmdlength);     
                    
   endsr;                      
 /end-free

* Procedure calls
      *
     d RUNCOMMAND      PR                  extpgm('RUNCOMMAND')
     d   CmdString                   30

     d RUNCOMMAND      PI
     d   CmdString                   30
      *
     d Command         pr                  EXTPGM('QCMDEXC')
     d  CmdString                    30
     d  CmdLength                    15  5
      *
      * Definitions
      *
     d CmdLength       s             15  5

      *
     c                   eval      *inlr = *on
        //=========================================
        //  *inzsr - One time run subroutine.
        //=========================================

      /free
        begsr *inzsr;
        cmdlength = %len(%trim(cmdstring));
        command(cmdstring : cmdlength);
        endsr;
      /end-free

QCMDEXC could also be called from CL , for instance to run a command dynamically when certain parameters that would be required are not know at run time. In this case calling QCMDEXC from a CL program would be an option. See the QCMDEXC snippet below

CHGVAR     VAR(&CMD) VALUE('CHGLIBL LIBL(' *CAT &LIBL +   
                          *TCAT ')')                                   
             CALL       PGM(QCMDEXC) PARM(&CMD &LEN) 

Managed as400 hosting

More examples of Using QCMDEXC in an as400 RPGLE Procedure to follow.

No comments:

Post a Comment