scripts

various script and utils
git clone git://z3bra.org/scripts
Log | Files | Refs

commit 0ece8960690d65022b65c79e7acd33df1d07cde6
parent a645bb4874435f0e83ec23fda311012107afe31e
Author: z3bra <willy@mailoo.org>
Date:   Fri Nov 28 12:32:48 +0100

removed bashism from memory

Diffstat:
memory | 45+++++++++++++++------------------------------
1 file changed, 15 insertions(+), 30 deletions(-)
diff --git a/memory b/memory @@ -1,4 +1,4 @@ -#!/bin/bash +#!/bin/sh # # z3bra - (c) wtfpl 2014 @@ -8,48 +8,33 @@ usage: $(basename $0) [-hptu] -h : print help -p : percentage of memory used (default) -t : total available memory - -u : memory used (human-readable) + -u : memory used EOF } # display the total of available memory in human readable format memtotal () { - read mem <<< `grep -E 'MemTotal' /proc/meminfo |awk '{print $2}'` - - if [ $mem -gt 1048576 ]; then - mem=`bc <<< "scale=2; $mem / 1048576"` - mem="${mem}G" - elif [ $mem -gt 1024 ]; then - mem=`bc <<< "$mem / 1024"` - mem="${mem}M" - fi - - echo $mem + grep MemTotal /proc/meminfo | awk '{print $2}' } -# display the memory used in human readable format +# display the memory used memused () { - read t f <<< `grep -E 'Mem(Total|Free)' /proc/meminfo |awk '{print $2}'` - read b c <<< `grep -E '^(Buffers|Cached)' /proc/meminfo |awk '{print $2}'` - mem=`bc <<< "($t - $f - $c - $b)"` - - if [ $mem -gt 1048576 ]; then - mem=`bc <<< "scale=2; $mem / 1048576"` - mem="${mem}G" - elif [ $mem -gt 1024 ]; then - mem=`bc <<< "$mem / 1024"` - mem="${mem}M" - fi - - echo $mem + t=$(grep MemTotal /proc/meminfo | awk '{print $2}') + f=$(grep MemFree /proc/meminfo | awk '{print $2}') + b=$(grep ^Buffers /proc/meminfo | awk '{print $2}') + c=$(grep ^Cached /proc/meminfo | awk '{print $2}') + + echo "($t - $f - $c - $b)" | bc } # display the memory used in percentage memperc () { - read t f <<< `grep -E 'Mem(Total|Free)' /proc/meminfo |awk '{print $2}'` - read b c <<< `grep -E '^(Buffers|Cached)' /proc/meminfo |awk '{print $2}'` - mem=`bc <<< "100 * ($t - $f - $c - $b) / $t"` + t=$(grep MemTotal /proc/meminfo | awk '{print $2}') + f=$(grep MemFree /proc/meminfo | awk '{print $2}') + b=$(grep ^Buffers /proc/meminfo | awk '{print $2}') + c=$(grep ^Cached /proc/meminfo | awk '{print $2}') + mem=`echo "100 * ($t - $f - $c - $b) / $t" | bc` echo "${mem}%" }