Thursday, March 5, 2015

bytes to gb/tb conversion

Often had to go to websites to do the bytes to KB.. TB.. GB conversions
here is the quick script for handy use

#!/bin/bash

# Give numeric value, which is in bytes
# will show all possible conversions 

call_bc() {
n1=$1
n2=$2
echo "scale=4; $n1/($n2)" |bc
}

nm=$1
pw=$2
pn=""

k_ilo=1024;
m_ega=$k_ilo*$k_ilo;
g_iga=$m_ega*$k_ilo;
t_era=$g_iga*$k_ilo;
p_eta=$t_era*$k_ilo;


[ -z "$nm" ] && exit

echo  "B: "$nm
for i in K M G T P; do
case $i in
K)pn=$k_ilo;;
M) let pn=$m_ega;;
G) let pn=$g_iga;;
T) let pn=$t_era;;
P) let pn=$p_eta;;
esac
echo -n "$i: "; call_bc $nm $pn
done

Usage and output
$ ./convert.sh 74274576
B: 74274576
K: 72533.7656
M: 70.8337
G: .0691
T: 0
P: 0

 

No comments:

Post a Comment