UNIX Command in SAP ABAP

SAPYard
Share on Facebook31Share on LinkedIn10Tweet about this on TwitterShare on Google+4
Please Share!!

Many times, we need to move an input file from one folder (say Input folder) to another folder (say Processed folder) after processing it. In such scenario in ABAP, we generally loop and read through the input file and transfer the input file data to the destination folder.

[adToAppearHere]

Demerit:
If the input file has large volume, then this process of moving the file from one folder to another by looping is time consuming and hits the performance of the system.

Another alternative is to create an External Operating System Command in SAP Transaction SM69 and use it in the program by calling the SAP standard FM ‘SXPG_COMMAND_EXECUTE’.

Demerit:
ABAPers usually do not have authorization to this Transaction SM69 and they need to depend on Basis/Security to get a new command created. Basis/Security in turn needs to have all the approvals in place to create these commands. Sometime due to audit issue, the external command cannot be created.

The smartest solution is to generate the UNIX Command in SAP ABAP programmatically and copy the input file instantly into the destination path.

Sample code to write UNIX command in SAP ABAP

* This is a code showing how to create and use COPY command of UNIX in ABAP
PARAMETERS:
* Input file path
p_input TYPE localfile,
* Processed file path
p_proc TYPE localfile.

* Declare the Types to file data
TYPES: BEGIN OF L_X_OUTPUT,
*sys*(200), ” Please note, there are asterisk before and after sys (i.e.*sys*)
END OF L_X_OUTPUT.

* * Internal table to store file data
DATA l_i_output TYPE STANDARD TABLE OF l_x_output WITH HEADER LINE.

* * Variable for the UNIX command
DATA: l_v_unix_comm(255) TYPE c.

* Copy command of UNIX
CONCATENATE ‘cp’ p_input p_proc
INTO l_v_unix_comm SEPARATED BY space.
* For example the Copy command is stored as below
* cp ‘/data/interfaces/input/input_file’ ‘/data/interfaces/processed/processed_file’
* Examples of UNIX Command *• mv filename1 filename2 — moves a file (i.e. gives it a different name, or moves it into a *different directory (see below) *• cp filename1 filename2 — copies a file

* Execute the UNIX Copy command.
* This command will copy the file from input file path to the processed file path
CALL ‘SYSTEM’ ID ‘COMMAND’ FIELD l_v_unix_comm
ID ‘TAB’
FIELD l_i_output-*sys*.

IF sy-subrc eq 0.
write: ‘File is copied successfully using UNIX command in ABAP’.
ENDIF.

Test Run:
Step1: Execute the above program


Step2: File is copied successfully
Validation
Step3: Input file which is to be copied
Step4: File which is copied successfully

Benefit of this Tip

1) No need to loop through the file again to copy it to another path.

2) No need to depend on BASIS/Security team to get a new External Operating System Commands created in T Code SM69 and no need to call the standard FM to use that Command.

3) We can write all the commands which are recognized by UNIX.

Image source : www.netcmdlets.com

Share on Facebook31Share on LinkedIn10Tweet about this on TwitterShare on Google+4
Please Share!!

5 Comments on "UNIX Command in SAP ABAP"

  1. I found that there is one class which can be used to copy app server file from one directory to same/different directory.

    CALL METHOD cl_cts_language_file_io=>copy_files_local
    EXPORTING
    im_source_file = ‘FINAL_I424_1.TXT’
    im_source_directory = ‘/tmp’
    im_target_file = ‘FINAL_I424_1_COPY.TXT’
    im_target_directory = ‘/tmp’
    EXCEPTIONS
    open_input_file_failed = 1
    open_output_file_failed = 2
    write_block_failed = 3
    read_block_failed = 4
    close_output_file_failed = 5
    others = 6
    .

  2. Steve Oldner | August 10, 2015 at 3:12 pm |

    We have several Z programs that use the unix commands. Just easier that way.

    • Thanks Steve.. True.. But in one of my client, the client’s reviewer strictly said us, we cannot call commands directly (i was not convinced though). They wanted it through SM69 via Basis. Not sure why ? 🙂

      Regards,
      Raju.

  3. Thanks for sharing information… Its working and resolve my issue.

Comments are closed.