Convert B to KiB, MiB calling a bash function -


in script i'm using check size of given mailbox, size returned in byte. more easy in "human readable" format.

this question great starting old , not able modify answers needs.

having:

mailbox="/var/mail/$1 good=471859200 #450mb actualsize=$(wc -c <"$mailbox") mailboxdim=$(grep "mailbox_size_limit" /etc/postfix/main.cf | awk -f" " '{print $3}') 

i need print value in kb / mb

if [ ! -f $mailbox ];   echo "can't check size of $mailbox | size=0; total=$mailboxdim"   exit 3 else   if [ $actualsize -lt $good ];     echo "size of mailbox $actualsize | size=$actualsize; total=$mailboxdim"     exit 0   fi fi 

well, code longer more or less same.

i'd need like

echo "size of mailbox convert($actualsize) | size=convert($actualsize); total=convert($mailboxdim)" 

and i'm not able write correct function

edit: everybody! if or in future wondering why need 2 variables achieve same result

actualsize=$(wc -c <"$mailbox") asize=$(convert "$actualsize") echo $asize 

is because use if compare values. asize "450mib" , if doesn't letters.

to solve rather use 2 variables:

good=450   if [ $actualsize -lt $good ];     echo "size $asize"   fi 

or use awk, then:

asize=$(convert $(wc -c <"$mailbox") | awk -f"mib" '{print $1}')   if [ $asize -lt $good ];     echo "size "$asize"mib"   fi 

or read manual: here suffix , iec / iec-i

convert() { numfmt --to=iec-i --suffix=b "$@"; } 

Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -