需求描述
1. MEM
在树莓派上,使用Shell编程,输出以下内存信息:
时间 | 总量 | 剩余量 | 当前占用(%) | 占用百分比动态平均值 |
---|---|---|---|---|
2018-01-12__15:00:28 | 1023M | 200M | 80.4% | 60.8% |
动态平均值=0.3动态平均值(上一次)+0.7当前占用比
在脚本编写过程中,可以参考以下内容:
awk '{printf("%s %s",$1,$2)}'
echo "scale=1;0.3*24+0.7*36" | bc
arry=(a c d e f)
arry=($1 $2)
验收标准
脚本完成后,用参数的形式向脚本传入上次的动态平均值。
执行后输出以下格式的输出:
s@l-PC:~/Documents/$ bash Memlog.sh 24
2019-07-17__22:58:39 7861M 6035M 23.2% 23.4%
测试代码
#!/bin/bash
DyAver=$1
if [[ ${DyAver}x == "x" ]]; then
exit 1
fi
MemValue=(`free -m | head -n 2| tail -n 1 | awk '{printf("%d %d", $3, $2)}'`)
MemLeft=$(echo "${MemValue[1]}-${MemValue[0]}" | bc)
MemAvaPrec=`echo "scale=1;${MemValue[0]}*100/${MemValue[1]}" | bc`
DyAver=`echo "scale=1;${DyAver}*0.3+${MemAvaPrec}*0.7" | bc`
NowTime=`date +"%Y-%m-%d__%H:%M:%S"`
echo "${NowTime} ${MemValue[1]}M ${MemLeft}M ${MemAvaPrec}% ${DyAver}%"