作者:gnuhpc
出处:http://www.cnblogs.com/gnuhpc/
#!/bin/bash ######################################################################### # Author: Futuredaemon # Created Time: Thu 19 Nov 2009 10:57:15 PM CST # File Name: checkMem.sh # Description: ######################################################################### #fetch and process memory information [ -f /proc/meminfo ] && { #First judge whether the file exists #-e是模式的意思,常用来保护以破折号开头的模式。 #-w是全字匹配。 #-i忽略大小写。 #-d指定分割符,-f为按照分割符取出的域,2-指的是域2及以后 Buffers=`grep -we 'Buffers' /proc/meminfo | cut -d' ' -f2- | tr -d "[A-Z][a-z] "` Cached=`grep -we 'Cached' /proc/meminfo | cut -d' ' -f2- | tr -d "[A-Z][a-z] "` MemFree=`grep -ie 'MemFree' /proc/meminfo | cut -d' ' -f2- | tr -d "[A-Z][a-z] "` MemTotal=`grep -ie 'MemTotal' /proc/meminfo | cut -d' ' -f2- | tr -d "[A-Z][a-z] "` SwapCached=`grep -ie 'SwapCached' /proc/meminfo | cut -d' ' -f2- | tr -d "[A-Z][a-z] "` SwapFree=`grep -ie 'SwapFree' /proc/meminfo | cut -d' ' -f2- | tr -d "[A-Z][a-z] "` SwapTotal=`grep -ie 'SwapTotal' /proc/meminfo | cut -d' ' -f2- | tr -d "[A-Z][a-z] "` } MEMUSED="$(( ( ( ( $MemTotal - $MemFree ) - $Cached ) - $Buffers ) / 1024 ))" MEMTOTAL="$(( $MemTotal / 1024))" MEMFREE="$(( $MEMTOTAL - $MEMUSED ))" MEMPER="$(( ( $MEMUSED * 100 ) / $MEMTOTAL ))" [ "$SwapTotal" -gt "1" ] && { SWAPUSED="$(( ( ( $SwapTotal - $SwapFree ) - $SwapCached ) / 1024 ))" SWAPTOTAL="$(( $SwapTotal / 1024))" SWAPFREE="$(( $SWAPTOTAL - $SWAPUSED ))" SWAPPER="$(( ( $SWAPUSED * 100 ) / $SWAPTOTAL ))" } || { SWAPUSED="0" SWAPTOTAL="0" SWAPPER="0" } # display the information /bin/echo /bin/echo "Memory" /bin/echo "Used: $MEMUSED MB" /bin/echo "Free: $MEMFREE MB" /bin/echo "Total: $MEMTOTAL MB" /bin/echo /bin/echo "Swap" /bin/echo "Used: $SWAPUSED MB" /bin/echo "Free: $SWAPFREE MB" /bin/echo "Total: $SWAPTOTAL MB" /bin/echo