2016-03-29 12 views
-1

Ich mache unter Skript aber Skript ausgeführt Ergebnis hat Fehler "Verwendung von nicht initialisierten Wert $ bay_name in Verkettung (.) Oder Zeichenfolge bei test.pl Zeile 92."Wie kann Skript ausgeführt werden?

#!/usr/bin/env perl 

use strict; 
use warnings; 
use JSON; 
use Getopt::Long; 
use POSIX qw/strftime/; 


## perl time variable 
my $dt = strftime('%Y%m%d%H%M%S',localtime); 


## Variable reset 
my ($key, $value, $bay_name, $dot) = ''; 
$dot = ' '; 

for (`cat test | awk 'NF' | awk /:/ | sed -e 's/ \+/ /g' -e 's/ : /:/g' | egrep "All|Bay|Number"`) 
    { 
     ($key, $value) = m/(.*?):(.*?)$/g 
      or next; 

     if ($key eq 'BayName') { 
      $bay_name = $value; 
     } 
     else { 
      $key = $bay_name . $dot . $key; 
     } 

     if ($key eq 'BayName') { 
      undef $key; 
     } 
     else { 
     print "\t,\n"; 
     print "\t{\n"; 
     print "\t\t\"{#HWNAMES}\":\"$key\",\n"; 
     print "\t\t\"{#HWSTATUS}\":\"$value\"\n"; 
     print "\t}\n"; 
    } 
    } 

ich Fix möchte es

Testdatei hat unter Inhalt

System Bay 

    Bay Name        : SB-1 
    Number of Standby Power Supplies  : 4 
    Number of Drive Enclosures   : 0 
    Number of Enclosure Slots   : 2 
    Number of MIBE Enclosures   : 2 

    Summary Status of Contained Modules 
    All Standby Power Supplies   : Normal 
    All Enclosures      : Normal 
    All Link Control Cards    : Normal 
    All Power Supplies     : Normal 
    All Enclosure Slots    : Normal 
    All Power Supplies     : Normal 
    All Fans       : Normal 
    All Management Modules    : Normal 
    All IO Module Carriers    : Normal 
    All Directors      : Normal 
    All MIBE Enclosures    : Normal 
    All Power Supplies     : Normal 

Drive Bays 

    Bay Name        : DB-1A 
    Number of Standby Power Supplies  : 8 
    Number of Drive Enclosures   : 16 

    Summary Status of Contained Modules 
    All Enclosures      : Normal 
    All Link Control Cards    : Normal 
    All Power Supplies     : Normal 
    All Standby Power Supplies   : Normal 

ich unten Ergebnis wollen, ist Bay Zeile Name in aus alle Inhalte ändern Schlag Format

setzen löschen
{ 
      "{#HWNAMES}":"SB-1 All Standby Power Supplies", 
      "{#HWSTATUS}":"Normal" 
    }, 
    { 
      "{#HWNAMES}":"SB-1 All Enclosures", 
      "{#HWSTATUS}":"Normal" 
    }, 
    { 
      "{#HWNAMES}":"SB-1 All Link Control Cards", 
      "{#HWSTATUS}":"Normal" 
    }, 
    { 
      "{#HWNAMES}":"SB-1 All Power Supplies", 
      "{#HWSTATUS}":"Normal" 
    }, 
    { 
      "{#HWNAMES}":"SB-1 All Enclosure Slots", 
      "{#HWSTATUS}":"Normal" 
    }, 
    { 
      "{#HWNAMES}":"SB-1 All Power Supplies", 
      "{#HWSTATUS}":"Normal" 
    }, 
    { 
      "{#HWNAMES}":"SB-1 All Fans", 
      "{#HWSTATUS}":"Normal" 
    }, 
    { 
      "{#HWNAMES}":"SB-1 All Management Modules", 
      "{#HWSTATUS}":"Normal" 
    }, 
    { 
      "{#HWNAMES}":"SB-1 All IO Module Carriers", 
      "{#HWSTATUS}":"Normal" 
    }, 
    { 
      "{#HWNAMES}":"SB-1 All Directors", 
      "{#HWSTATUS}":"Normal" 
    }, 
    { 
      "{#HWNAMES}":"SB-1 All MIBE Enclosures", 
      "{#HWSTATUS}":"Normal" 
    }, 
    { 
      "{#HWNAMES}":"SB-1 All Power Supplies", 
      "{#HWSTATUS}":"Normal" 
    } 

Unten ist mein Skript

for (`/opt/emc/SYMCLI/bin/symcfg -sid $sid list -env_data`) { 

     if (/^\s+Bay Name\s+:\s+(\S+)$/){ 
       $bay_name = $1; 

     } elsif (/(Number.*)\s+:\s+(\d+)/){ 
       print "\t{\n"; 
       print "\t\t\"{#HWNAMEC}\":\"$bay_name $1\",\n"; 
       print "\t\t\"{#HWCOUNT}\":\"$2\"\n"; 
       print "\t},\n"; 


     } elsif (/(All.*)\s+:\s+(\S+)/) { 
       print "\t{\n"; 
       print "\t\t\"{#HWNAMES}\":\"$bay_name $1\",\n"; 
       print "\t\t\"{#HWSTATUS}\":\"$2\"\n"; 
       print "\t},\n"; 
     } 
} 
+0

Vielleicht entfernen Sie den 'tail -n +5 | head -n -1' Teil, um den Rest der Datei zu bearbeiten ... – xxfelixxx

+0

Ihr Code metnions "BayName", aber die Ausgabe Ihrer Katze ist "Bay Name", so dass Sie die Bay-Variable nie wirklich setzen, da Sie sie nie erreichen. Sie sollten Data Dumper verwenden, um Ihre Variablen zu überprüfen –

Antwort

1

ohne alle Shell-Pipes und Funktionen zu verwenden, könnten Sie dies mit ein wenig Perl allein erreichen.

use strict; 
use warnings; 

my $bay_name; 
while (<DATA>) { 
    chomp(); 
    if (/^\s+Bay Name\s+:\s+(\S+)$/){ 
     $bay_name = $1; 
    } elsif (/(Number of Standby Power Supplies)\s+:\s+(\d+)/){ 
     print "$bay_name $1 : $2\n"; 
    } elsif (/(All Standby Power Supplies|All Enclosures)\s+:\s+(\S+)/) { 
     print "$bay_name $1 : $2\n"; 
    } 
} 


__DATA__ 
System Bay 

    Bay Name        : SB-1 
    Number of Standby Power Supplies  : 4 

    Summary Status of Contained Modules 
    All Standby Power Supplies   : Normal 

Drive Bays 

    Bay Name        : DB-1A 
    Number of Standby Power Supplies  : 8 

    Summary Status of Contained Modules 
    All Enclosures      : Normal 

Dies erzeugt die folgende Ausgabe auf der Grundlage Ihrer Beispieldaten

SB-1 Number of Standby Power Supplies : 4 
SB-1 All Standby Power Supplies : Normal 
DB-1A Number of Standby Power Supplies : 8 
DB-1A All Enclosures : Normal 
+0

Vielen Dank. Ich möchte ändern elseif "Alle" Inhalt und "Nummer". Wie kann man machen? – ZXC

+0

'elsif (/ (Anzahl) \ s +: \ s + (\ d +) /) { print" $ bay_name $ 1: $ 2 \ n "; } elsif (/ (Alle) \ s +: \ s + (\ S +) /) { print "$ bay_name $ 1: $ 2 \ n"; } ' – ZXC

+0

müssten Sie' elsif (/ (Anzahl. *) \ S +: \ s + (\ d +) /) {'und' elsif (/(All.*)\s+:\s+(\S+) verwenden /) {'das passt zu Zeilen, die All und Number enthalten. Ich bin mir jedoch nicht sicher, warum Sie dies tun sollten, es sei denn, Ihre Datei enthält andere Zeilen, die Sie nicht angezeigt haben und die Sie ebenfalls erfassen möchten. –

1

Die eigentliche Frage, die Sie wird durch Zugabe von

fixiert fragte
my $bay_name; 

in Ihrem for Schleife. Sie haben use strict aktiviert, was eine gute Vorgehensweise ist und eine Deklaration von Variablen erfordert, aber keine Deklaration von $bay_name, auf die sich die Fehlermeldung bezieht.

Andere haben jedoch darauf hingewiesen, dass der Aufruf einer Pipeline von Perl völlig unnötig ist und Sie den Code auf verschiedene andere Arten verbessern können.

Verwandte Themen