Registration

Dear SAP Community Member,
In order to fully benefit from what the SAP Community has to offer, please register at:
http://scn.sap.com
Thank you,
The SAP Community team.
Skip to end of metadata
Go to start of metadata

Table of contents

Introduction

This gives an overview on how to create the External Commands using SM49, assigning UNIX (or other operating system) file or using a UNIX Command and implementing them. Below given is the step by step approach to the same.

Example: Listing the Files in the required directory.

Create the command in SM49

Navigate to SM49 and Create an External Command

  • In the Command section, the Operating System field is protected but may be changed by pressing the search help (F4)
  • In the Definition section, for Operating System Command text box we can even give the file path of application server file. All the UNIX coding can be done in the file. So when ever this external command is called, the functionality written in the file gets triggered. For this example write 'ls'.

Create the ABAP program which calls SXPG_COMMAND_EXECUTE

Once the Command is created, then use function module 'SXPG_COMMAND_EXECUTE' as shown below.

Be Careful

The operatingsystem parameter is case sensitive, it must be exactly the same value as in SM69. For example, you must enter "Linux" instead of "LINUX".

Error rendering macro 'code': Invalid value specified for parameter 'lang'
CONSTANTS: c_extcom    TYPE sxpgcolist-name VALUE 'Z_LIST_FILES',
           c_oper      TYPE syopsys VALUE 'UNIX'.

DATA: v_dir_input      TYPE sxpgcolist-parameters.  " Input Directory
DATA: t_result         TYPE STANDARD TABLE OF btcxpm.

v_dir_input = './sap/tmp'.

CALL FUNCTION 'SXPG_COMMAND_EXECUTE'
  EXPORTING
    commandname                   = c_extcom
    additional_parameters         = v_dir_input
    operatingsystem               = c_oper
  TABLES
    exec_protocol                 = t_result
  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.

Result

T_RESULT will contain the list of files in the specified directory after execution of the function module.

Example of result:

LENGTH

MESSAGE

10

adm_script

7

backups

3

bin

4

boot

4

data

3

dev

7

distrib

3

etc

If the command fails, you would get something like that:

LENGTH

MESSAGE

40

ls: ./sap/tmp: No such file or directory

44

External program terminated with exit code 1

This way we can make use of UNIX coding in SAP.

Other links

4 Comments

  1. Hello Sitharamaraju Kakarlapudi,

    thank you for adding Content to the SCN Wiki. I have two suggestions to you:

    • Redo the screen shots as they are not readable
    • exchange the screenshot of the code with  the actual code and choose the "Code" formating from the Format dropdown.

    Best regards
    Gregor

  2. Hi Gregor, and Sitharamaraju,

    if you don't mind, I'll do it this time (just to check my authorizations as wiki editor for the first time!)

    Sandra

  3. Thank you for this.  One problem I'm running into with SAP and file operations is the original developers assumed there would be very small file names and file paths.

    Function module SXPG_COMMAND_EXECUTE' accepts only 255 characters for optional parameters.

    If I am trying to copy a file which is nested in a directory that is 200 characters in length to another, it will simply truncate.

    How can we work around this shortcoming?  Can we nest command execution to do a "cd".  Or, is there some way to alias a path name or something?

    Thanks for any suggestions you can offer here.

  4. Hi,
     
    Can you tell me what's exit code 32? I can't seem to find documentation for it anywhere.
     
    Is there a place where I can get all the exit codes? I've checked the Function Module SXPG_COMMAND_EXECUTE. 
     
    Thanks for help.