Thread shell Befehl umsetzen (13 answers)
Opened by wenze at 2018-05-22 11:14

Linuxer
 2018-05-22 17:48
#188427 #188427
User since
2006-01-27
3869 Artikel
HausmeisterIn

user image
Hi,

ich würde mir erstmal Gedanken machen, was konkret abgedeckt werden soll.

Mir scheint, Du willst eine oder mehrere Profil-Datei(en) eines SAP-Systems auslesen.

Laut https://help.sap.com/saphelp_nw70ehp1/helpdata/en/... scheint es eine Zeile der Art SAPDBHOST = hs0011 zu sein, die Dich interessiert.

1. Wenn es wirklich um "SAPDBHOST" geht, würde ich das auch als Suchstring nehmen. Vor allem, wenn es im Programm hart kodiert wird und nicht von außen übergeben wird. Selbst dann würde ich wohl daran festhalten.

2. Wenn die Formatierung zwingend einer Zuweisung entspricht, also ein Format VARIABLE = wert hat, würde ich das für die Suche auch ausnutzen.

Mit diesem Wissen würde ich anders an die Suche und an die Subroutine dafür gehen.

Wieder ungetestet:
Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
use strict;
use warnings;
use 5.010;


sub extract_dbhosts {
    my @files = @_;
    my @dbhosts;
    my %seen;

    my $search = 'sapdbhost';   # keep it lower case

    # https://help.sap.com/saphelp_nw70ehp1/helpdata/en/c4/3a6136505211d189550000e829fbbd/frameset.htm

    FILE:
    for my $file ( @files ) {
        open my $fh, '<', $file or die "open($file,ro) failed: $!";
            
        LINE:
        while ( my $line = <$fh> ) {
            
            # skip not matching lines
            next LINE if index( lc($line), $search, 0 ) < 0;
    
            # get value of assignment
            my ( $dbhost ) = (split m{\s*=\s*}, $line )[1];

            # remove whitespaces (newlines) from dbhost
            $dbhost =~ s/\s+//g;

            # add db hosts in order of occurence        
            push @dbhosts, $dbhost  if not $seen{$dbhost}++;

            # assuming only one dbhost per file; otherwise comment out the "next FILE;"
            next FILE;
        }

        close $fh;
    }

    # return list of db hosts in order of occurence
    return @dbhosts;
}


### use case:

# get all profile files; SID is replaced with *
my @profile_files = glob( "/usr/sap/*/SYS/profile/DEFAULT.PFL" );

# search through all profile files and get db_hosts
my @db_hosts = extract_dbhosts( @profile_files );


edit: Erweitert: Leerzeichen/Newlines aus $dbhost entfernen; Danke an haj für den Hinweis
Last edited: 2018-05-22 20:12:23 +0200 (CEST)
meine Beiträge: I.d.R. alle Angaben ohne Gewähr und auf Linux abgestimmt!
Die Sprache heisst Perl, nicht PERL. - Bitte Crossposts als solche kenntlich machen!

View full thread shell Befehl umsetzen