bvstone

Calling a QSH Command from RPG

Posted:

Calling a QSH Command from RPG

In a recent project I was working on I had to call a few commands using QSH from my RPG programs.  So, I created a function to do this.  It looks like the following:

////////////////////////////////////////////////////////////////*
// #cmd_runQSHCommand - Run a QShell Command                   *
////////////////////////////////////////////////////////////////*
dcl-proc #cmd_runQSHCommand export;
  dcl-pi *N int(10);
    In_command char(1024) const;
    In_debugFile char(256) const;
    Out_errorMsg char(256);
  end-pi;

  // Work Variables
  dcl-s QCmdCmd char(32000);
  dcl-s l_errMsg char(256);
  dcl-s rc int(10);
  dcl-s l_debug char(4);

  l_debug = #getControlValue('DEBUG');

  // This makes anything other than zero return an escape message which should
  //  be trapped by QCMDEXC
  QCmdCmd = 'ADDENVVAR ENVVAR(QIBM_QSH_CMD_ESCAPE_MSG) VALUE(Y) REPLACE(*YES)';
  callp(e) #QCmdExc(QCmdCmd:%len(%trimr(QCmdCmd)));
  
  if (l_debug <> '*YES');
    QCmdCmd = 'ADDENVVAR ENVVAR(QIBM_QSH_CMD_OUTPUT) VALUE(NONE) LEVEL(*JOB) ' + 
              'REPLACE(*YES)';
  else;
    QCmdCmd = 'ADDENVVAR ENVVAR(QIBM_QSH_CMD_OUTPUT) VALUE(''FILE=' +
              %trim(in_debugFile) + 
              ''') LEVEL(*JOB) REPLACE(*YES)';
  endif;

  callp(e) #QCmdExc(QCmdCmd:%len(%trimr(QCmdCmd)));

  // This should set the CCSID of files created by QSHELL to a specific value.
  QCmdCmd = 'ADDENVVAR ENVVAR(QIBM_CCSID) VALUE(' + 
            %trim(#getControlValue('QSH_CCSID')) + 
            ') LEVEL(*JOB) REPLACE(*YES)';
  callp(e) #QCmdExc(QCmdCmd:%len(%trimr(QCmdCmd)));

  QCmdCmd = 'STRQSH CMD(''' + %trim(in_command) + ''')';
  callp(e) #QCmdExc(QCmdCmd:%len(%trimr(QCmdCmd)));

  if (%error);
    l_errMsg = 'Error running command ' + %trim(in_command) + '.';
    exsr $Error;
  endif;

  QCmdCmd = 'RMVENVVAR ENVVAR(QIBM_CCSID)';
  callp(e) #QCmdExc(QCmdCmd:%len(%trimr(QCmdCmd)));

  QCmdCmd = 'RMVENVVAR ENVVAR(QIBM_QSH_CMD_OUTPUT)';
  callp(e) #QCmdExc(QCmdCmd:%len(%trimr(QCmdCmd)));

  QCmdCmd = 'RMVENVVAR ENVVAR(QIBM_QSH_CMD_ESCAPE_MSG)';
  callp(e) #QCmdExc(QCmdCmd:%len(%trimr(QCmdCmd)));

  return 0;

//***************************************************************
//* Return Error
//***************************************************************
begsr $Error;

  Out_errorMsg = l_errMsg;
  return -1;

endsr;

end-proc #cmd_runQSHCommand;

 

The first thing we do is get the control value for our application to see if it's in debug mode.  You may have another way to do this, but I like to use control files for this so that there is no hardcoding.

Next, we add an environment variable named QIBM_QSH_CMD_ESCAPE_MSG and set it to the value of Y so that anything that doesn't complete with a success code of 0 will throw an escape message.

Now, if our application is in debug mode we set the environment variable QIBM_QSH_CMD_OUTPUT to the appropriate setting so we can store the results of the command to review if needed.

Next we use the QIBM_CCSID environment variable to set the CCSID for any IFS stream files created by the QSH command.  Again, in this case we're retrieving this as a control value (normally 1208 or 1252).  Now, I haven't gotten this to work yet, and the CCSID of the files are always created with my job's CCSID (37) but hopefully it's just a PTF required for my system.

Finally, we run the command passed into the function using QSHELL (QSH).  Because we set the system to use escape messages for anything other than a successful command we can use the %error Built in Function (BIF) to trap any errors and report them.

The last part, which may not be necessary in your case, is to remove the environment variables we set up.  

 




Latest Posts:

G4MSMAIL Now Allows Wildcard Attachments G4MSMAIL Now Allows Wildcard Attachments
Posted by June 10, 2021
BVSTools >> BVSTools Announcements >> GreenTools for Microsoft Apps (G4MS) Specific Announcements
G4G Update for Uploading Large Files G4G Update for Uploading Large Files
Posted by May 28, 2021
BVSTools >> BVSTools Announcements >> GreenTools for G Suite (Google Apps) (G4G) Specific Announcements
Can We Purchase License Keys for More Than One Year at a Time? Can We Purchase License Keys for More Than One Year at a Time?
Posted by May 21, 2021
BVSTools >> BVSTools Announcements
JOBWATCH 5.10 Released with New Feature to Monitor CPU Percentages JOBWATCH 5.10 Released with New Feature to Monitor CPU Percentages
Posted by May 10, 2021
BVSTools >> BVSTools Announcements >> Job Watch (JOBWATCH) Specific Announcements
Iseries Access data transfer Iseries Access data transfer
Posted by April 29, 2021
IBM Power Systems >> (QGPL) IBM i
YAJL - Parsing YAJL - Parsing
Posted by April 4, 2021
Programming >> RPG Programming
Create LPAR partition and install OS Create LPAR partition and install OS
Posted by March 24, 2021
IBM Power Systems >> (QGPL) IBM i
G4MS Now Allows User/Password Authentication Option G4MS Now Allows User/Password Authentication Option
Posted by March 16, 2021
BVSTools >> BVSTools Announcements >> GreenTools for Microsoft Apps (G4MS) Specific Announcements
IPDS printer file using CHRSIZ IPDS printer file using CHRSIZ
Posted by February 25, 2021
Programming >> RPG Programming
How to Correctly Send the Display Machine Information (DSPMCHINF) Data to BVSTools for Key Generation How to Correctly Send the Display Machine Information (DSPMCHINF) Data to BVSTools for Key Generation
Posted by February 9, 2021
BVSTools >> BVSTools Software Discussion
G4MS Updated to v7.10 - Required Update for All G4MS Users G4MS Updated to v7.10 - Required Update for All G4MS Users
Posted by November 12, 2020
BVSTools >> BVSTools Announcements >> GreenTools for Microsoft Apps (G4MS) Specific Announcements
MAILTOOL Errors with Microsoft Office 365 (2020) MAILTOOL Errors with Microsoft Office 365 (2020)
Posted by September 3, 2020
BVSTools >> BVSTools Software Discussion >> Email Tools (MAILTOOL) Specific Discussion
Using MAILTOOL With Office 365 and Two Factor Authentication (2FA or MFA) Using MAILTOOL With Office 365 and Two Factor Authentication (2FA or MFA)
Posted by August 17, 2020
BVSTools >> BVSTools Software Discussion >> Email Tools (MAILTOOL) Specific Discussion
Updating DNS With Dynamic IP Addresses After a Router Reboot/Power Outage Updating DNS With Dynamic IP Addresses After a Router Reboot/Power Outage
Posted by August 16, 2020
Programming >> Proof of Concept (POC)
GETURI v10.10 Released with New ILE Functions GETURI v10.10 Released with New ILE Functions
Posted by August 12, 2020
BVSTools >> BVSTools Announcements >> Get URI (GETURI) Specific Announcements

Reply




Copyright 1983-2020 BVSTools
GreenBoard(v3) Powered by the eRPG SDK, MAILTOOL Plus!, GreenTools for Google Apps, jQuery, jQuery UI, BlockUI, CKEditor and running on the IBM i (AKA AS/400, iSeries, System i).