Copyright 2024 - BV TallVision IT

Is there a way to execute a Unix command and see the results, as if it was executed from a Unix prompt ? There is, with a little help of a small report. 

Executing a Unix command can be dangerous as you can destroy relevant files and kill relevant processes. So make sure this test report doesn't get transported across the system landscape - if you don't want to end up on the company blacklist for system-murder.

Unix statements can be executed on server level when the system is running on a Unix server. The statements that are executed will be started as the SAP system user, which is likely to be someone with serious authorizations, so again: be careful. So why would you want to use this ? You can use AL11 to browse the directory structure of the server and view files and you can use CG3Z (from PC to backend) and CG3Y (from backend to PC) to place or fetch files. But then: not all directories are available from AL11 and you may want to do more than browse and read/write. Copy ? Move (rename) ? Delete ? Check connections ?

REPORT ZABAPCADABRA_UNIX.

selection-screen: begin of line, COMMENT 1(10) lbl_01 FOR FIELD pa_cmd.
parameters pa_cmd type char200 lower case default 'ls -l /usr/sap/'.
selection-screen: end of line.

INITIALIZATION.
  lbl_01 = 'Command'.

START-OF-SELECTION.

  DATA: lt_result_output TYPE TABLE OF char200.
* __      __                 _                CALL 'SYSTEM' ID 'COMMAND' ...
* \ \    / /__ _  _ _  _ _  (_) _ _   __ _    This is a very HAZARDOUS system command
*  \ \/\/ // _` || '_|| ' \ | || ' \ / _` |   NEVER make this available on a
*   \_/\_/ \__,_||_|  |_||_||_||_||_|\__, |   productive system
*                                    |___/
  CALL 'SYSTEM' ID 'COMMAND' FIELD pa_cmd ID 'TAB' FIELD lt_result_output.
  loop at lt_result_output into pa_cmd.
    write: / pa_cmd.
  endloop.

You will have to know a thing or 2 about Unix commands to be able to use this command window. A few common statements that can be useful here:

  • ls -l /usr/sap/ shows a directory listing of the files and directories in /usr/sap/, in the following format:
    total 7
    drwxr-xr-x    7 hrdadm   sapsys          256 Jan 20 15:42 HRD
    drwxr-xr-x    3 hrdadm   sapsys          256 Sep 01 2015  SYS
    drwxr-x       5 root     sapsys          256 Sep 01 2015  hostctrl
    drwxrwxr-x    6 hrdadm   sapsys          256 Jan 12 13:49 interface
    drwxr-xr-x    2 root     system          256 Jul 06 2015  lost+found
    -rwxr-xr-x    1 root     sapsys          484 Mar 07 2016  sapservices
    -rwxr-x       1 root     sapsys          165 Sep 01 2015  sapservices.1
    
    The first drwxr-xr-x settings indicate d=Directory and rwx = Read Write eXecute, which is repeated 3 times for the group owners.
  • cat /usr/sap/sapservices show/list the content of the /usr/sap/sapservices file
  • mv /usr/sap/sapservices /usr/sap/backup/sapservices move the file from /usr/sap/sapservices file to the backup directory
  • cp /usr/sap/sapservices /usr/sap/backup/sapservices copy the file /usr/sap/sapservices to the backup directory
  • rm /usr/sap/backup/sapservices delete the backup file
  • help returns the following information:
    If available, you can refer to the Base Document Library
    for general assistance.
    Some basic Commands are:
        man -k keyword#- lists commands relevant to a keyword
        man command##- prints out the manual pages for a command
        cat###- concatenates files (and just prints them out)
        vi###- text editor
        ls###- lists contents of directory
        mail##- sends and receives mail
        passwd##- changes login password
        sccshelp##- views information on the Source Code Control System
        smit##- system management interface tool
        tset##- sets terminal modes
        who###- who is on the system
        write##- writes to another user
    To find programs about mail, use the command: ##man -k mail
    and print out the man command documentation via:#man mail
    You can log out of the system by typing: ##exit
    
  • man rm or manual for the rm command, this statement can be used for all Unix commands, man ls,man kill, man cp anything you may need the manual for..
    rm Command
    Purpose
           Removes (unlinks) files or directories.
    Syntax
           rm [  -f ] [ -r ] [ -R ] [ -i ] [ -e ] File ...
    Description
           The rm command removes the entries for the
           specified File parameter from a directory. If an
           entry is the last link to a file, the file is then
           deleted. If you do not have write permission for a
           file and the standard input is a terminal, you are
           prompted with the file name and ask to confirm that
           you want to delete the file. If you type a y (for
           yes), the file is deleted, type any other character
           and the file is not deleted. You do not need read
    
    ... this text continues.
  • One other thing you may find interesting: command redirecting and command piping. The > sign can be used to redirect the output of a Unix command to a file. The demonstration report is already doing this, and the output results are listed as a report, but these results can also be (re)directed to a file. Thus ls - l > myfile.txt will create a file with the output from ls - l. A double pipe like ls - l >> myfile.txt will append to the file. A similar setup can be done with "piping", for which the | is used, the output of a command can be "handed over" to the next command with a pipe.