给出以下命令及其输出:
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 文件系统 尺寸 已使用 可用 容量 已装入 在 fsystem是/ dev / blocks是da3 95G
33G 58G 36%/使用的是容量安装是fsystem是tmpf块是
499M 88K 499M 1%/ dev / used是hm有用的是容量安装是fsystem是/ dev / blocks是da1
124M 39M 79M 33%/ boot使用是有用的是装载容量
答案 0 :(得分:2)
就像daxim已经指出的那样:解析df
的输出是一个愚蠢的想法,但如果我不得不破解一个小脚本,它可能看起来像这样:
df -B M -P /path1 /path2 /path3 | sed -e '1d' | sort -k 4,4 -r -h | column -t