File operations in SAP Application server (AL11) using UNIX Command

Applies to:

SAP BW 3.5, BI 7.0


Summary

This paper demonstrates how to implement basic file operations like move, merge and delete files in SAP Application server (AL11) by calling UNIX script file through OS commands and to automate the OS command through process chain.

Introduction


UNIX shell


A Unix shell is a command language interpreter, the primary purpose of which is to translate command lines typed at a terminal into system actions. The shell itself is a program through which other programs are invoked. The shell has some built-in functions, which it performs directly, but most commands that you enter cause the shell to execute programs that are external to the shell. This sets the shell apart from other command interpreters, because it is just another user program at the same time that it functions almost exclusively as a mechanism for invoking other programs. With Shell we can use standard UNIX commands as building blocks to create new commands.


External Commands

An external command is a pre-defined operating system command that can be executed within the SAP system. Both the maintenance and execution of external commands is protected with SAP authorizations. You can maintain and execute external commands in dialog (from the CCMS menu) or in ABAP programs, using special function modules. External commands can also be executed as a step in a background job.

As the CCMS administrator, you can modify commands and activate additional security mechanisms without having to change the program. You can add your own commands and parameters to the variety of (pre-defined) commands delivered by SAP

Business Scenario

  There can be scenarios to perform basic file operations (like move, merge, Zip, delete etc.) on files generated in AL11 via Open hub destinations or APDs. These operations can be achieved in SAP BW by direct use of UNIX commands in OS command or calling UNIX script file through OS command.

Consider that an open hub destination is generating .csv file in AL11. Every time when you run the DTP for that Open hub it will overwrite the existing file which was already generated. So if we need to retain the previously generated file, then every time when a file is generated we can move it to a new file with time stamp in the file name. There can be a requirement to merge files generated in AL11 to a single file before sending out of SAP BW. Similarly as a part of data retention & clean up activity it may be required to delete older files.

Procedure to implement the above scenarios

In this approach we will create a UNIX script file (UNIX commands for the file handling operations are written in this file) and access this UNIX script file through OS command.

Following steps will demonstrate how to achieve the above mentioned scenarios.

Step 1:

Create UNIX script file containing UNIX commands for performing the required file handling operations.

Write the UNIX script in notepad and save it as “.sh” file format.

Below provided sample code will merge two files (FILE1 and FILE 2) into a single file with a new name (MERGERFILE) and timestamp. It will also delete files with name starting ‘MERGEFILE’ in AL11 older than 30 days.

Sample code:

timestamp=$( date +%Y%m%d%H%M%S )              */ Assign server time stamp to variable timestamp.   

   

cat /var/opt/sapbi/FILE1.txt  /var/opt/sapbi/FILE2.txt  > /var/opt/sapbi/ MERGEFILE”$timestamp”.txt

*/ Combine the data in FILE1 & FILE2 present the path /var/opt/sapbi/ into a new file and the new file will have name MERGEFILE preceded by the timestamp at which it was generated.

find /var/opt/sapbi/ -type f -name ‘ MERGEFILE*.txt.gz’ -mtime +29 -exec rm {} \;

*/ Files having name starting with MERGEFILE and older than 30 days present in the path /var/opt/sapbi/ in AL11 will be deleted

Upload the unix script file into Application server (AL11). 

Function module: ARCHIVFILE_CLIENT_TO_SERVER can be used to upload.

UNIX command syntax used and its meaning:

cat [path of file1] [path of file2] > [path of merged file] : This command will merge the mentioned files present in the given path and make a new merged file in the mentioned path with the given name.

gzip [path of file]   : this command will zip the file.

Step 2:

Go to Tcode SM69 and click on create button to create OS command as shown below:

1.PNG

Enter the details:

Command name: Give a name for the command.

Operating system command: sh

Parameters for operating system command: enter the UNIX script file path.                                               

Command sh will trigger/execute the script file mentioned in the parameter box.


2.PNG

Below are the screenshots of application server:

UNIX script file “Script.sh” is uploaded in AL11


3.PNG

FILE1 & FILE2 present in AL11. This may be generated by open hub destinations or APDs or flat file loads.


4.PNG


Data in FILE1:


5.PNG


Data in FILE2:


6.PNG

Click on “Execute” button after opening the OS command in SM69 to execute it.


7.PNG


Merge file generated in AL11 after execution of the OS command.


8.PNG


Data in Merge File:


9.PNG


If gzip command is used, then generated file will be zipped and will have extension as.gz


10.PNG


If this file handling has to be done after the execution of Open hub DTPs or APD execution or flat file load via process chain, then this can be added in to the process chain flow.

OS command process type in Process chain can be used for this.


11.PNG


Inside the process variant enter the OS command Name & details which was already created.


12.PNG


13.PNG


So when the process chain executes this step, it will trigger the OS command & which in turn will trigger the UNIX script “script.sh” and the file handling operations will be carried out.

To report this post you need to login first.

10 Comments

You must be Logged on to comment or reply to a post.

  1. Mohan Babu K J

    Hi Vishnu,

    Thanks for sharing. Do you have any similar wiki for decrypting gpg file? I’m not able to execute from SM69. Do I need to have Script file for that as well?

    Regards,

    MB

    (0) 

Leave a Reply