Forums >> Programming >> RPG Programming

Jump to:




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.  

 




les.lieurance.jr@outlook.com

RE: Calling a QSH Command from RPG

Posted:

RE: Calling a QSH Command from RPG

Been studying your example. I'm wanting to use QSH to SFTP in batch.  How do i check for specific errors?


Last edited 08/29/2024 at 07:30:35



bvstone

RE: RE: Calling a QSH Command from RPG

Posted:

RE: RE: Calling a QSH Command from RPG

Sorry, I can't help with that one.  SFTP isn't native to the IBMi (it is with the open source tools).


Last edited 08/29/2024 at 07:54:28



les.lieurance.jr@outlook.com

RE: Calling a QSH Command from RPG

Posted:

RE: Calling a QSH Command from RPG

I can monitor for QSH005 and I can see it shows the error message exit status of 255, but that doesn't give me details.  Is there a way to get the actual exit code or the specific reason it failed?


Last edited 09/02/2024 at 14:43:15



bvstone

RE: RE: Calling a QSH Command from RPG

Posted:

RE: RE: Calling a QSH Command from RPG

Probably not without logging the data to a file and reading it.


Last edited 09/02/2024 at 15:44:45




Reply




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