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:

Microsoft Office 365 Servers and Random Errors Issue Microsoft Office 365 Servers and Random Errors Issue
Posted by November 14, 2022
BVSTools >> BVSTools Software Discussion >> Email Tools (MAILTOOL) Specific Discussion
Sending/Resending Emails Using a MIME File with MAILTOOL Sending/Resending Emails Using a MIME File with MAILTOOL
Posted by November 8, 2022
BVSTools >> BVSTools Software Discussion >> Email Tools (MAILTOOL) Specific Discussion
Sending an HTML Email on Your IBM i Using MAILTOOL Sending an HTML Email on Your IBM i Using MAILTOOL
Posted by November 1, 2022
BVSTools >> BVSTools Software Discussion >> Email Tools (MAILTOOL) Specific Discussion
Transferring License Keys from One System to Another Transferring License Keys from One System to Another
Posted by October 31, 2022
BVSTools >> BVSTools Software Discussion
Calculating the Size of a File Before Base64 Encoding Calculating the Size of a File Before Base64 Encoding
Posted by August 13, 2022
Programming >> RPG Programming
GreenTools for Microsoft Apps (G4MS) v9.12 Now Includes Function to Send Emails using MIME File GreenTools for Microsoft Apps (G4MS) v9.12 Now Includes Function to Send Emails using MIME File
Posted by August 11, 2022
BVSTools >> BVSTools Announcements >> GreenTools for Microsoft Apps (G4MS) Specific Announcements
GreenTools for Google Apps (G4G) v15.20 Now Supports Shortcuts GreenTools for Google Apps (G4G) v15.20 Now Supports Shortcuts
Posted by August 6, 2022
BVSTools >> BVSTools Announcements >> GreenTools for G Suite (Google Apps) (G4G) Specific Announcements
GreenTools for Microsoft Apps (G4MS) Groups Admin Authority Instructions GreenTools for Microsoft Apps (G4MS) Groups Admin Authority Instructions
Posted by July 26, 2022
BVSTools >> BVSTools Software Discussion >> GreenTools for Microsoft Apps (G4MS) Specific Discussion
GreenTools for Microsoft Apps (G4MS) v9.10 Now Includes OneDrive Functions that Work With Groups/Shared Drives GreenTools for Microsoft Apps (G4MS) v9.10 Now Includes OneDrive Functions that Work With Groups/Shared Drives
Posted by July 19, 2022
BVSTools >> BVSTools Announcements >> GreenTools for Microsoft Apps (G4MS) Specific Announcements
GreenTools for Google Apps (G4G) v15.10 Now Includes Drive Functions that Work With Shared Drives GreenTools for Google Apps (G4G) v15.10 Now Includes Drive Functions that Work With Shared Drives
Posted by July 15, 2022
BVSTools >> BVSTools Announcements >> GreenTools for G Suite (Google Apps) (G4G) Specific Announcements
GreenTools for Microsoft Apps (G4MS) v9.00 Now Offers Functions to Bypass Registration Command and BVSTools Landing Page GreenTools for Microsoft Apps (G4MS) v9.00 Now Offers Functions to Bypass Registration Command and BVSTools Landing Page
Posted by July 4, 2022
BVSTools >> BVSTools Announcements >> GreenTools for Microsoft Apps (G4MS) Specific Announcements
What Objects Should I Omit from Replication to Ensure My License Keys Work on my HA/DR System? What Objects Should I Omit from Replication to Ensure My License Keys Work on my HA/DR System?
Posted by June 27, 2022
BVSTools >> BVSTools Software Discussion
GreenTools for Google Apps (G4G) v15.00 Now Offers Functions to Bypass Registration Command and BVSTools Landing Page GreenTools for Google Apps (G4G) v15.00 Now Offers Functions to Bypass Registration Command and BVSTools Landing Page
Posted by May 3, 2022
BVSTools >> BVSTools Announcements >> GreenTools for G Suite (Google Apps) (G4G) Specific Announcements
How Do I Switch From MAILTOOL Plus to GreenTools for Google (OAuth 2.0) or Microsoft Office 365? How Do I Switch From MAILTOOL Plus to GreenTools for Google (OAuth 2.0) or Microsoft Office 365?
Posted by April 18, 2022
BVSTools >> BVSTools Software Discussion >> Email Tools (MAILTOOL) Specific Discussion
PTFs Issued for SSL/TLS Issues PTFs Issued for SSL/TLS Issues
Posted by March 12, 2022
IBM Power Systems >> PTF Watch

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).