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:

GreenTools for Slack (G4SLK) v3.00 Released GreenTools for Slack (G4SLK) v3.00 Released
Posted by January 17, 2020
BVSTools >> BVSTools Announcements >> GreenTools for Slack (G4SLK) Specific Announcements
Calling a QSH Command from RPG Calling a QSH Command from RPG
Posted by December 26, 2019
Programming >> RPG Programming
SPLTOOL Print Range (PRTRNG) Function Updated to Handle Spooled Files up to 999,999,999 Pages SPLTOOL Print Range (PRTRNG) Function Updated to Handle Spooled Files up to 999,999,999 Pages
Posted by December 14, 2019
BVSTools >> BVSTools Announcements >> Spooled File Tools (SPLTOOL) Specific Announcements
GreenTools for Microsoft Apps (G4MS) Updated to v6.00 - Now Uses Microsoft Graph APIs GreenTools for Microsoft Apps (G4MS) Updated to v6.00 - Now Uses Microsoft Graph APIs
Posted by November 24, 2019
BVSTools >> BVSTools Announcements >> GreenTools for Microsoft Apps (G4MS) Specific Announcements
V7R4 Changes CCSID of Compressed File Using PASE JAR Command - Here's The Fix V7R4 Changes CCSID of Compressed File Using PASE JAR Command - Here's The Fix
Posted by November 21, 2019
IBM Power Systems >> (QGPL) IBM i
Using GETURI to Make OAuth 2.0 Requests - Custom Headers for Access Tokens Using GETURI to Make OAuth 2.0 Requests - Custom Headers for Access Tokens
Posted by November 11, 2019
BVSTools >> BVSTools Software Discussion >> Get URI (GETURI) Specific Discussion
GreenTools for Microsoft Apps (G4MS) v5.00 Released with Updated OneDrive Support and 3rd Party Functionality GreenTools for Microsoft Apps (G4MS) v5.00 Released with Updated OneDrive Support and 3rd Party Functionality
Posted by October 20, 2019
BVSTools >> BVSTools Announcements >> GreenTools for Microsoft Apps (G4MS) Specific Announcements
BVSTools is Now Running V7R4M0 BVSTools is Now Running V7R4M0
Posted by September 28, 2019
BVSTools >> BVSTools Announcements
GreenTools for G Suite (G4G) Now Includes Send Mail Functionality (G4GSMAIL Addon) GreenTools for G Suite (G4G) Now Includes Send Mail Functionality (G4GSMAIL Addon)
Posted by August 27, 2019
BVSTools >> BVSTools Announcements >> GreenTools for G Suite (Google Apps) (G4G) Specific Announcements
GreenTools For G Suite (G4G) v12.00 Released With Base OAuth 2.0 Functionality GreenTools For G Suite (G4G) v12.00 Released With Base OAuth 2.0 Functionality
Posted by July 28, 2019
BVSTools >> BVSTools Announcements >> GreenTools for G Suite (Google Apps) (G4G) Specific Announcements
BVSTools Small Price Increase in 2020 BVSTools Small Price Increase in 2020
Posted by July 26, 2019
BVSTools >> BVSTools Announcements
GreenTools for Vertex Cloud (VTXCLOUD) Now Available GreenTools for Vertex Cloud (VTXCLOUD) Now Available
Posted by July 22, 2019
BVSTools >> BVSTools Announcements >> GreenTools for Vertex Cloud (VTXCLOUD) Specific Announcements
GreenTools for Google Apps (G4G) - Drive Addon Successfully Verified by Google GreenTools for Google Apps (G4G) - Drive Addon Successfully Verified by Google
Posted by July 22, 2019
BVSTools >> BVSTools Announcements >> GreenTools for G Suite (Google Apps) (G4G) Specific Announcements
Why I Cancelled my DynDNS Service and How I Replaced It with an IBM i Application Why I Cancelled my DynDNS Service and How I Replaced It with an IBM i Application
Posted by July 17, 2019
IBM Power Systems >> (QGPL) IBM i
Green Tools for G Suite (G4G) Product Updates (Licensing, Functionality, Base Product) Green Tools for G Suite (G4G) Product Updates (Licensing, Functionality, Base Product)
Posted by July 13, 2019
BVSTools >> BVSTools Announcements >> GreenTools for G Suite (Google Apps) (G4G) 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).