-2

给定以下命令及其输出:

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";

输出如下

内容类型:text/htmln Filesystem Size Used Avail Capacity Mounted on fsystem is /dev/ blocks is da3 95G
33G 58G 36% /

used 是avail 容量是mounted 是fsystem 是tmpf blocks 是
499M 88K 499M 1% /dev/ used 是hm

可用性是安装容量是 fsystem 是 /dev/ 块是 da1
124M 39M 79M 33% /boot

used 是avail 是容量是mounted 是

4

1 回答 1

2

就像 daxim 已经指出的那样:解析 的输出是一个愚蠢的主意df,但如果我不得不破解一个小脚本,它可能看起来像这样:

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

于 2012-04-26T17:44:10.057 回答