Demand:
Write a script that calculates the amount of memory that is consumed by all processes in the Linux system. (hint, use PS or TOP command)
#!/bin/bashtime= ' date +%y-%m-%d-%h:%m:%s ' # #方法一s =0for m in ' ps aux |awk ' { print $6} ' |grep -v ' RSS ' Do ((s+= $m)) doneecho "$time All processes memory-intensive and for $s k " echo "==========================" # #方法二sum =0for q in ' ps aux |awk ' { print $6} ' |grep -v ' RSS ' dosum=$[$sum + $q]doneecho "$time All processes consume memory and are $sum k " echo " ======================== " # #方法三top -bn1 | sed ' 1,7 ' d >top.txtecho "$time all processes that occupy memory and for ' awk ' {(zh=zh+$6)}; end {print zh} ' top.txt ' k "echo " ========================= "# #方法四n = ' top -bn1 |sed ' 1,7 ' d |awk ' {(su=su+$6)}; END {PRINT SU} ' echo ' $time all processes that occupy memory and for $n k "echo " ========================= "# # Method Five d= ' ps aux |grep -v ' rss tty ' |awk ' {(sum=sum+$6)}; End&nbSp {print sum} ' echo ' $time All processes are memory-intensive and $d k "
Shell exercises to count memory usage