Capture Return value of Script in CL Program

120 pts.
CL Program
CL programming
Script errors
Hi I am running a script file which returns some value at the end of the script. i.e. the last line of my script is "exit returnVal". The value is also populating in the returnVal variable correctly but I don't know how can we capture this return value in my cl program. Could anyone please help me in capturing this value in cl program.



Answer Wiki

Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

Discuss This Question: 5  Replies

There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.
  • DanTheDane
    Pls be more specific. Is your script started from a CL-pgm ? Pls provide relevant extract from your code. DanF
    2,555 pointsBadges:
  • TomLiotta
    It's not clear what you might see without knowing a little more about your CL and the STRQSH command as well as the script. However, there's a good chance the message ID QSH0005 will be returned to your CL program as a *COMP message. The message description is in message file QZSHMSGF in library QSHELL. You should be able to receive that message and access the exit status in the message data. It's returned as a 4-byte binary field. Tom
    125,585 pointsBadges:
  • VivekSharma
    The script that I have created is as below: #!/bin/bash # This is a SFTP script success=0 sftp -oPort=6222 -oUserKnownHostsFile=/dev/null -oStrictHostKeyChecking=no User@TargetSys || success=$?<<EOF cd folder1 put fileName EOF #End of script exit $success I am trying to capture the value error if any in success field. How ever I am facing the issue that the script runs only the first line i.e. "sftp -b .........." command and gives the value to the success field. The rest of the commands are not executed. The next issue that I am facing is how to capture this return value in my cl program. At present my CL code is: PGM /* IF VARIABLES WERE SET PREVIOUSLY, REMOVE THEM SO THAT OUR + ADDENVVAR COMMANDS WORK. */ DCL VAR(&QSCOMD) TYPE(*CHAR) LEN(200) + VALUE(*BLANKS) CHGVAR VAR(&QSCOMD) VALUE('ksh -x') /* SET THE COMMAND OUTPUT TO NONE, AND TELL QSH TO ISSUE + ESCAPE MESSAGES FOR ERRORS. */ ADDENVVAR ENVVAR(QIBM_QSH_CMD_OUTPUT) VALUE(NONE) + LEVEL(*JOB) REPLACE(*YES) ADDENVVAR ENVVAR(QIBM_QSH_CMD_ESCAPE_MSG) VALUE(Y) + LEVEL(*JOB) REPLACE(*YES) /* EXECUTE OUR QSHELL COMMAND */ STRQSH CMD(&QSCOMD) MONMSG MSGID(QSH0000) EXEC(DO) SNDPGMMSG MSGID(CPF9897) MSGF(QCPFMSG) MSGDTA('QShell + command failed!') ENDDO ENDPGM
    120 pointsBadges:
  • TomLiotta
    Here's a very basic example of the general technique:
       dcl   &MsgId       *char    7
       dcl   &MsgDta      *char  128
       dcl   &ExitSts     *int           value( 0 )
       qsh cmd('exit 79')
       rcvmsg      msgtype( *LAST ) rmv( *NO ) msgdta( &MsgDta ) +
                     msgid( &MsgId )
       if ( &MsgId *eq 'QSH0005' )  do
          chgvar   &ExitSts     %bin( &MsgDta 1 4 )
    You should be able to paste that into a CL source member, compile it and run it. It runs the most basic QShell command to set an EXIT status of "79". After the QSH command returns, the CL receives the last message on the program's message queue to see if it's QSH0005. If it is, it pulls the 4-byte binary value from the received message data. The DMPCLPGM command then dumps the program variables to show that the first four bytes of &MsgDta contain the hex value of the EXIT status and that the &ExitSts variable contains the integer representation of those four bytes. The EXIT command that is run by QSH should have the same value. You could use RCVMSG after your MONMSG-ENDDO. The MONMSG DO-group would catch exceptions from your STRQSH command, and RCVMSG would receive whatever the last message was. You should probably also put a RETURN statement inside of your MONMSG DO-group so that statemants after the ENDDO aren't executed. There are lots of other ways to take care of that, but a RETURN is straightforward and clear to anyone.
    125,585 pointsBadges:
  • WilsonAlano
    Try this #!/bin/bash # This is a SFTP script sftp -oPort=6222 -oUserKnownHostsFile=/dev/null -oStrictHostKeyChecking=no User@TargetSys cd folder1 put fileName #End of script exit $? Regards, Wilson
    2,710 pointsBadges:

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

Thanks! We'll email you when relevant content is added and updated.


Share this item with your network: