您的位置:首页 > 运维架构 > Shell

shell脚本中变量的递归使用--真实内存使用查看脚本

2017-09-05 14:04 477 查看
脚本中进行变量计算时,需要加一个[ ],在外面再套一个$

[root@ES25 tmp]# cat real_mem_used01.sh
#!/bin/bash
TOTAL=`free -g|grep Mem |awk '{print $2}'`
USED=`free -g|grep Mem |awk '{print $3}'`
BUFFER=`free -g|grep Mem |awk '{print $6}'`
CACHE=`free -g|grep Mem |awk '{print $7}'`
real_used=$[$USED - $BUFFER - $CACHE]
PUSED=$[$real_used*100/$TOTAL]
echo $PUSED%
[root@ES25 tmp]#
[root@ES25 tmp]# sh real_mem_used01.sh
12%
[root@ES25 tmp]#

方法二:
[root@ES25 tmp]# cat aa.sh
#!/bin/bash
Real_used=`free -g|grep cache |grep ":"|awk '{print $3}'`
Total=`free -g|grep Mem|awk '{print $2}'`
awk 'BEGIN{printf"%.2f\n",('$Real_used'/'$Total')*100}'
[root@ES25 tmp]# sh aa.sh
10.64
[root@ES25 tmp]#
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  内存 脚本 shell