bash bytes coneverter to human

b2h()
{
# By: Simon Sweetwater
# Spotted Script @: http://www.linuxjournal.com/article/9293?page=0,1
# Convert input parameter (number of bytes)
# to Human Readable form
#
SLIST="bytes,KB,MB,GB,TB,PB,EB,ZB,YB"

POWER=1
VAL=$( echo "scale=2; $1 / 1" | bc)
VINT=$( echo $VAL / 1024 | bc )
while [ ! $VINT = "0" ]
do
let POWER=POWER+1
VAL=$( echo "scale=2; $VAL / 1024" | bc)
VINT=$( echo $VAL / 1024 | bc )
done

echo $VAL$( echo $SLIST | cut -f$POWER -d, )
}

k2h()
{
# Convert input parameter (number of kilobytes)
# Spotted Script @: http://www.linuxjournal.com/article/9293?page=0,1
# to Human Readable form
# MODIFIED BY kossboss
SLIST="bytes,KB,MB,GB,TB,PB,EB,ZB,YB"
POWER=1
VAL=$( echo "scale=2; $1 * 1024 / 1" | bc)
VINT=$( echo $VAL / 1024 | bc )
while [ ! $VINT = "0" ]
do
let POWER=POWER+1
VAL=$( echo "scale=2; $VAL / 1024" | bc)
VINT=$( echo $VAL / 1024 | bc )
done
echo $VAL$( echo $SLIST | cut -f$POWER -d, )
}

Leave a Reply

Your email address will not be published. Required fields are marked *