Tuesday, May 6, 2008

Sample ABAP Program to Execute Unix command from within SAP

REPORT YSMT018A
MESSAGE-ID YL.
* ABAP to append ribesnsl to ribes
* and remove input file using sxpg_command_execute

DATA: FILE1(25) VALUE '/vmedata/???/file1nsl'.
DATA: FILE2(25) VALUE '/vmedata/???/file2'.
DATA: W_MESSAGE(50).
DATA: RLBES LIKE RLBES.
FILE1+9(3) = SY-SYSID.
FILE2+9(3) = SY-SYSID.
* sxpg_command_execute parameters
DATA: REMOVE_FILE LIKE SXPGCOLIST-PARAMETERS.
DATA: PROTOCOL LIKE BTCXPM OCCURS 0.
*
OPEN DATASET FILE2 FOR APPENDING IN TEXT MODE MESSAGE W_MESSAGE.
IF SY-SUBRC NE 0 .
MESSAGE E114 WITH FILE2 W_MESSAGE.
ENDIF.
OPEN DATASET FILE1 FOR INPUT IN TEXT MODE MESSAGE W_MESSAGE.
IF SY-SUBRC NE 0.
MESSAGE E114 WITH FILE1 W_MESSAGE.
ENDIF.

DO.
READ DATASET FILE1 INTO RLBES.
IF SY-SUBRC NE 0.
EXIT.
ENDIF.
TRANSFER RLBES TO FILE2.
IF SY-SUBRC NE 0.
MESSAGE E009 WITH FILE2 SY-SUBRC.
ENDIF.
ENDDO.

MESSAGE I114 WITH FILE1 'appended'.
***----------------------------------------------------------------****

DATA: COMMAND3(60)
* VALUE 'rm /vmedata/???/rlbesnsl' .
VALUE 'rm /vmedata/???/file1nsl' .

COMMAND3+12(3) = SY-SYSID.


*submit the unix command remove file1
REMOVE_FILE = COMMAND3+3.
* create y_remove command in sm69
CALL FUNCTION 'SXPG_COMMAND_EXECUTE'
EXPORTING
COMMANDNAME = 'Y_REMOVE'
* OPERATINGSYSTEM = SY-OPSYS
* TARGETSYSTEM = SY-HOST
* STDOUT = 'X'
* STDERR = 'X'
* TERMINATIONWAIT = 'X'
* TRACE = ' '
ADDITIONAL_PARAMETERS = REMOVE_FILE
* IMPORTING
* STATUS =
TABLES
EXEC_PROTOCOL = PROTOCOL
EXCEPTIONS
NO_PERMISSION = 1
COMMAND_NOT_FOUND = 2
PARAMETERS_TOO_LONG = 3
SECURITY_RISK = 4
WRONG_CHECK_CALL_INTERFACE = 5
PROGRAM_START_ERROR = 6
PROGRAM_TERMINATION_ERROR = 7
X_ERROR = 8
PARAMETER_EXPECTED = 9
TOO_MANY_PARAMETERS = 10
ILLEGAL_COMMAND = 11
WRONG_ASYNCHRONOUS_PARAMETERS = 12
CANT_ENQ_TBTCO_ENTRY = 13
JOBCOUNT_GENERATION_ERROR = 14
OTHERS = 15.

IF SY-SUBRC = 0.
MESSAGE I114 WITH FILE1 'deleted'.
ENDIF.

No comments:

Blog Archive