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";
}
}
Vielleicht entfernen Sie den 'tail -n +5 | head -n -1' Teil, um den Rest der Datei zu bearbeiten ... – xxfelixxx
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 –