2012-04-26 55 views
-2

考慮下面的命令及其輸出:DF輸出用perl

ssh -q $server 'df -hP /raj* 

Size Used Avail Capacity Mounted On 
200G 154G 44G 79% /raj_day 
200G 154G 44G 49% /raj1_day 
200G 154G 44G 39% /raj2_day 

我想轉換成表格格式,以便演示應該不錯。另外,要按排序順序顯示容量。我在Perl中的任何提示?

我的腳本如下...

############################################# 
#!/usr/local/bin/perl 

# Use either -h or -k flag or leave it blank for default (-k) 
# -h for human-readable size output 
# -k for 1k-block output 
$flag = "-h"; 
@df = `df $flag`; 

print "Content-type: text/htmln\n"; 
print "<table border=2>\n"; 
print "<tr>\n"; 

print "<td><b>Filesystem</b></td>\n"; 

if ($flag eq "-h") { 
    print "<td><b>Size</b></td>\n"; 
} 
else { 
    print "<td><b>1k-blocks</b></td>\n"; 
} 

print "<td><b>Used</b></td>\n"; 
print "<td><b>Avail</b></td>\n"; 
print "<td><b>Capacity</b></td>\n"; 
print "<td><b>Mounted on</b></td>\n"; 
print "</tr>\n"; 

foreach $line (@df) { 
    next if ($line =~ /Filesystem/); 

    ($fsystem,$blocks,$used,$avail,$capacity,$mounted) = split(/s+/,$line); 

    print "fsystem is $fsystem\n"; 
    print "blocks is $blocks\n"; 
    print "used is $used\n"; 
    print "avail is $avail\n"; 
    print "capacity is $capacity\n"; 
    print "mounted is $mounted\n"; 

($number,$sign) = split(/%/,$capacity); 
if ($number < 60) { 
    print "<tr bgcolor=green>\n"; 
} 
elsif (($number >= 60) && ($number < 90)) { 
    print "<tr bgcolor=yellow>\n"; 
} 
else { 
    print "<tr bgcolor=red>\n"; 
} 
# 
print "<td>$fsystem</td>\n"; 
print "<td>$blocks</td>\n"; 
print "<td>$used</td>\n"; 
print "<td>$avail</td>\n"; 
print "<td>$capacity</td>\n"; 
print "<td>$mounted</td>\n"; 
print "</tr>\n"; 
} 

print "</table>\n"; 

如下輸出

內容類型:文本/ htmln 文件系統尺寸使用 擔保條件容量安裝 上 FSYSTEM是/ dev /塊是DA3 95G
33G 58G 36%/

使用的果是容量被安裝在FSYSTEM是TMPF塊是
499M 88K 499M 1%的/ dev /使用是HM

果是容量被安裝是FSYSTEM是/ dev /塊是DA1
124M 39M 79M 33%/使用的果是容量被安裝引導

+5

郵政代碼你已經嘗試過了。 – EricM 2012-04-26 15:11:49

+5

提示:[不要解析'df'輸出,這是一個愚蠢的想法。](http://stackoverflow.com/questions/6350466/regex-help-on-unix-df#comment-7437792) – daxim 2012-04-26 15:18:12

+1

@jack - 哦。剛剛做到了。剛到這個地方。 – 2012-04-26 15:25:30

回答

2

像daxim已經指出:這是一個愚蠢的想法來解析的df輸出,但如果我不得不劈倒一個小的腳本的話,大概是這樣的:

df -B M -P /path1 /path2 /path3 | sed -e '1d' | sort -k 4,4 -r -h | column -t