我有一個將df解析爲perl可以使用的東西的腳本。在Perl哈希中使用未初始化的值
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4
5 my @headers = qw(name size used free capacity mount);
6 my @df = `df -k`;
7 shift @df; # get rid of the header
8
9 my %devices;
10 for my $line (@df) {
11 my %info;
12 @info{@headers} = split /\s+/, $line; # note the hash slice
13 $info{capacity} = _percentage_to_decimal($info{capacity});
14 $devices{ $info{mount} } = \%info;
15 }
16
17 # Change 12.3% to .123
18 sub _percentage_to_decimal {
19 my $percentage = shift;
20 $percentage =~ s{%}{};
21 return $percentage/100;
22 }
23 # Now the information for each device is in a hash of hashes.
24
25 # Show how much space is free in device /dev/ad4s1e
26 print $devices{"/production/log"}{free} ;
27 print "\n";
28 for my $info (values %devices) {
29 # Skip to the next device if its capacity is not over 60%.
30 next unless $info->{capacity} > .10;
31
32 # Print some info about each device
33 printf "%s is at %d%% with %dK remaining.\n",
34 $info->{mount}, $info->{capacity}*100, $info->{free};
35 }
但是我不斷收到這些警告。
Use of uninitialized value in substitution (s///) at ./get_df line 21.
Use of uninitialized value in division (/) at ./get_df line 22.
Use of uninitialized value in hash element at ./get_df line 15.
Use of uninitialized value in substitution (s///) at ./get_df line 21.
Use of uninitialized value in division (/) at ./get_df line 22.
Use of uninitialized value in hash element at ./get_df line 15.
Use of uninitialized value in substitution (s///) at ./get_df line 21.
Use of uninitialized value in division (/) at ./get_df line 22.
Use of uninitialized value in hash element at ./get_df line 15.
Use of uninitialized value in substitution (s///) at ./get_df line 21.
Use of uninitialized value in division (/) at ./get_df line 22.
Use of uninitialized value in hash element at ./get_df line 15.
Use of uninitialized value in substitution (s///) at ./get_df line 21.
Use of uninitialized value in division (/) at ./get_df line 22.
Use of uninitialized value in hash element at ./get_df line 15.
9006792
/production/log is at 70% with 9006792K remaining.
/is at 37% with 17037532K remaining.
/production is at 11% with 13171728K remaining.
/export/home is at 24% with 11199904K remaining.
/production/archive is at 18% with 8095796K remaining.
/boot is at 28% with 68351K remaining.
更新: 我昨晚看了DF模塊CPAN在家裏,但我必須讓系統管理員批准,把它安裝。 在DF文件系統太長,所以它被打印到另一行。這弄亂了數據轉儲打印輸出 - 一些散列值被標記爲undef。
[email protected]]:~/.wjohnson> df -k
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
28313732 9816924 17035356 37%/
/dev/sda1 101086 27516 68351 29% /boot
tmpfs 2987896 0 2987896 0% /dev/shm
/dev/mapper/VolGroupPROD-ExportHome
15481840 3495504 11199904 24% /export/home
/dev/mapper/VolGroupPROD-Production
15481840 1523692 13171716 11% /production
/dev/mapper/VolGroupPROD-ProdLog
30963708 20410952 8979892 70% /production/log
/dev/mapper/VolGroupPROD-ProdArchive
10313016 1693640 8095500 18% /production/archive
[[email protected]]:~/.wjohnson>
[[email protected]]:~/.wjohnson>
[[email protected]]:~/.wjohnson>
[[email protected]]:~/.wjohnson>
[[email protected]]:~/.wjohnson> df -k | grep -v dev
Filesystem 1K-blocks Used Available Use% Mounted on
28313732 9816924 17035356 37%/
15481840 3495504 11199904 24% /export/home
15481840 1523692 13171716 11% /production
30963708 20410952 8979892 70% /production/log
10313016 1693640 8095500 18% /production/archive
[[email protected]]:~/.wjohnson>
從Data :: Dumper--很多哈希值都是未定義的。有沒有一種方法可以預先定義散列的值。我想學會擺脫他們。
$VAR1 = {};
Use of uninitialized value in substitution (s///) at ./get_df.just_capacity line 24.
Use of uninitialized value in division (/) at ./get_df.just_capacity line 25.
Use of uninitialized value in hash element at ./get_df.just_capacity line 17.
$VAR1 = {
'' => {
'free' => undef,
'mount' => undef,
'used' => undef,
'name' => '/dev/mapper/VolGroup00-LogVol00',
'capacity' => '0',
'size' => undef
}
};
$VAR1 = {};
$VAR1 = {
'' => {
'free' => undef,
'mount' => undef,
'used' => undef,
'name' => '/dev/mapper/VolGroup00-LogVol00',
'capacity' => '0',
'size' => undef
},
這是通過使用df -k | grep -v var
解決 - 但必須有更好的方法。
輕微挑剔,但代碼中和錯誤中的行號都是1。 – ThisSuitIsBlackNot
您是否嘗試過使用Data :: Dumper在運行時檢查%info中的內容?我懷疑用df,你會發現設備名稱可能會變長,並且強制剩下的數據放在下一行,因此會對哈希中未初始化的數據提出警告。 編輯:看着df的幫助,我想你可能想要通過-P選項。 – razzmataz
當我在我的機器上運行這個命令時,我沒有收到任何錯誤,但是接下來'df'在我們的每臺機器上都返回不同的東西。 –