#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 "Swap"
/bin/echo "Used: $SWAPUSED MB"
/bin/echo "Free: $SWAPFREE MB"
/bin/echo "Total: $SWAPTOTAL MB"
/bin/echo