求助一个 nmon 的数据处理小脚本,里头 step2 函数的操作看不懂,主要是 x 变量不知道什么意思

#!/bin/sh
OS=`uname`
step1()   
{
echo `date` >> res
echo "****************************************************************************************************************" >> res
case $OS in 
     AIX)
echo "USER%" "SYS%" "TOTAL%" "MEM%" "IOBUSY%" "DISK" "NMON_FILENAME">> res1;;
    Linux)
echo "NMON_FILENAME" "USER%" "SYS%" "TOTAL%" "MEM%" "MAX_DISKBUSY%" "DISK" >> res1;;
esac
}


step2()  
{
awk '{for(i=1;i<=NF;i++)a[NR,i]=$i;x=x<NF?NF:x}END{for(i=1;i<=x;i++){for(j=1;j<=NR;j++){if(length(a[j,i]))printf a[j,i]" "};printf "\\n"}}' tmp >> res2
rm -rf  tmp
}


step3()
{
case $OS in 
     AIX)
awk '{for(i=1;i<=NF;i++)printf "%-7s ",$i;print ""}' res1 >> res;;
    Linux)
    
column -t res1 >> res;;
esac
rm -rf filename res1 res2
}
cpu()     
{
cat $1 | grep "CPU_ALL" | grep -v "User"|grep -v  "PCPU_ALL"|grep -v "SCPU_ALL" | awk -F , '{print $3,$4}' > cpu
cat cpu | awk '{sum+=$1} END {printf "%.2f\\n",sum/NR}' >> tmp
cat cpu | awk '{sum+=$2} END {printf "%.2f\\n",sum/NR}' >> tmp
cat cpu | awk '{sum+=$1+$2} END {printf "%.2f\\n",sum/NR}' >> tmp
rm -rf cpu
}
mem()
{
case $OS in 
     AIX)
cat $1 | grep "MEM" |grep T.... |grep -v "MEMNEW" |grep -v "MEMUSE"|grep -v ps | awk -F , '{print $3}' > mem
cat mem | awk '{sum+=$1} END {printf "%.2f\\n",100-sum/NR}' >> tmp;;
   Linux)
cat $1 | grep "MEM" |grep T.... |grep -v "MEMNEW" |grep -v "MEMUSE" |grep -v ps| awk -F , '{print $3,$7,$12,$15}' > mem
cat mem | awk '{sum+=($2+$3+$4)} END {printf "%.2f\\n",100-100*sum/NR/$1}' >> tmp
rm -rf mem >> res1;;
esac
}
disk()    
{
grep DISKBUSY $1 > d1

awk -F[,] '{$1="";$2="";print $0}' d1 > d2
sed -n '1p' d2 > d3
awk 'NR>1' d2 > d4
awk '{for(i=1;i<=NF;i++){a[i]+=$i}}END{for(j=1;j<=NF;j++){ printf"%.2f", a[j]/NR" ";printf "\\n"}}' d4 > d5
awk '{for(i=1;i<=NF;i++)a[NR,i]=$i;x=x<NF?NF:x}END{for(i=1;i<=x;i++){for(j=1;j<=NR;j++){if(length(a[j,i]))printf a[j,i]" "};printf "\\n"}}' d5 >> d3
awk '{for(i=1;i<=NF;i++)a[NR,i]=$i;x=x<NF?NF:x}END{for(i=1;i<=x;i++){for(j=1;j<=NR;j++){if(length(a[j,i]))printf a[j,i]" "};printf "\\n"}}' d3 >> d3fz
awk '{print $2,$1}' d3fz |sort -n -r | sed -n '1p'|awk '{for(i=1;i<=NF;i++)print $i}' >> tmp
rm -rf d1 d2 d3 d4 d5 d3fz 
}
step_s()
{
case $OS in 
     AIX)
cpu $1
mem $1
disk $1
echo "$1" >> tmp;;
   Linux)
echo "$1" >> tmp
cpu $1
mem $1
disk $1;;
esac
}
step_d()
{
ls | grep .nmon > filename
cat filename | while read A
do
step_s $A
step2
done
cat res2 >> res1
}

rm -rf res     
                         
if [ -d ./$1 ];then
cd $1
step1
step_d
step3

mv res ../res

cat ../res
 
elif [ -s ./$1 ];then
step1
step_s $1     
step2
cat res2 >> res1
step3
cat res
else
echo "NMONμþ»òþ¼в»Õȷ"
fi

step2函数的操作看不懂,主要是x变量不知道什么意思

关闭