Surprising to me this isnt a program yet…

Anyhow the credit goes all to: http://www.linuxjournal.com/article/9293?page=0,1 – Simon Sweetwater is the author of this script – good job if your reading this 🙂


#!/bin/ksh




PS he did mention there is a slight mistake in the script:

Oops mistake above

Simon Sweetman's picture
Submitted by Simon Sweetman (not verified) on Fri, 09/17/2010 – 20:22.

As soon as you make a post you find a mistake, the above while statement should be changed to

To avoid numeric overflows in some Korn shell implementations.

 




My modifications

b2h – bytes to hr – no difference between b2h and bytestohr()
k2h – the kilobytes that it takes in are base 2 kilobytes (or in other words they are technically kibibytes) – where 1 kilobyte = 1024 bytes
#!/bin/bash
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 -gt 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 infotinks
    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 -gt 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, )
}



If you get an overflow error try this:

#!/bin/bash
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 infotinks
    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, )
}
 



example of use:

# b2h 5000 4.88KB
# k2h 5000 4.88MB
to unset the functions from the shell – so they dont work anymore and they are out of memory:
# unset k2h
# unset b2h
likewise for the very top scripts
# unset bytestohr

Leave a Reply

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