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:

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 or Microsoft Office 365? How Do I Switch From MAILTOOL Plus to GreenTools for Google 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
Google Dropping Support for Google Dropping Support for "Less Secure Apps" May 30th, 2022. What Does This Mean for Your IBM i Email?
Posted by March 4, 2022
BVSTools >> BVSTools Software Discussion >> Email Tools (MAILTOOL) Specific Discussion
Have You Installed a New Version of MAILTOOL and Now Things Are Acting Different?  Check the Command Defaults! Have You Installed a New Version of MAILTOOL and Now Things Are Acting Different? Check the Command Defaults!
Posted by February 28, 2022
BVSTools >> BVSTools Software Discussion >> Email Tools (MAILTOOL) Specific Discussion
Using MAILTOOL Plus on V7R1, or Any OS Using TLS 1.1 or Older Using MAILTOOL Plus on V7R1, or Any OS Using TLS 1.1 or Older
Posted by January 27, 2022
BVSTools >> BVSTools Software Discussion >> Email Tools (MAILTOOL) Specific Discussion
BVSTools ILE Functions Being Updated to Remove Hashtag (#) from Function Names BVSTools ILE Functions Being Updated to Remove Hashtag (#) from Function Names
Posted by December 30, 2021
BVSTools >> BVSTools Announcements
GETURI v12.00 Released Removing Beginning # (Hashtag) From Function Names GETURI v12.00 Released Removing Beginning # (Hashtag) From Function Names
Posted by December 28, 2021
BVSTools >> BVSTools Announcements >> Get URI (GETURI) Specific Announcements
Is any of BVSTools Software Affected by the log4j exploit? Is any of BVSTools Software Affected by the log4j exploit?
Posted by December 20, 2021
BVSTools >> BVSTools Software Discussion
GreenTools for Microsoft Apps (G4MS) Updated to Allow Downloads, Deletes, and Sharing of Files GreenTools for Microsoft Apps (G4MS) Updated to Allow Downloads, Deletes, and Sharing of Files
Posted by December 17, 2021
BVSTools >> BVSTools Announcements >> GreenTools for Microsoft Apps (G4MS) Specific Announcements
SSL Handshake Errors with GETURI, MAILTOOL and GreenTools Products SSL Handshake Errors with GETURI, MAILTOOL and GreenTools Products
Posted by October 18, 2021
BVSTools >> BVSTools Software Discussion
MAILTOOL Updated to Retry Sending when GSK SSL Handshake Error 415 (GSK_ERROR_BAD_PEER) is Encountered MAILTOOL Updated to Retry Sending when GSK SSL Handshake Error 415 (GSK_ERROR_BAD_PEER) is Encountered
Posted by August 19, 2021
BVSTools >> BVSTools Announcements >> eMail Tool (MAILTOOL) Specific Announcements
MAILTOOL Updated to Allow List-Unsubscribe and User Defined Headers MAILTOOL Updated to Allow List-Unsubscribe and User Defined Headers
Posted by August 13, 2021
BVSTools >> BVSTools Announcements >> eMail Tool (MAILTOOL) Specific Announcements
AWS signing process in as400 AWS signing process in as400
Posted by August 13, 2021
Programming >> Web Programming

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