From: TJ Luoma <luomat@gmail.com>
To: Zsh-Users List <zsh-users@zsh.org>
Subject: Converting bash script to zsh for converting bytes to 'readable'
Date: Wed, 12 Sep 2018 18:02:26 -0400 [thread overview]
Message-ID: <CADjGqHtKCEc-cwrQXOtjVSrYE6U+tpP5aYWrfd9prrVmvOKrtg@mail.gmail.com> (raw)
[-- Attachment #1: Type: text/plain, Size: 1833 bytes --]
Several years ago I came across this script (below) as a 'bash' script. I
believe the comments came from the original as well:
BYTES="$@"
#MBYTES='1024'
MBYTES='1000'
# Array of suffixes
declare -a METRIC=(' Bytes' 'KB' 'MB' 'GB' 'TB' 'XB' 'PB')
# magnitude of 2^10
MAGNITUDE=0
# change this numeric value to increase decimal precision
PRECISION="scale=1"
# numeric arg val (in bytes) to be converted
UNITS=$(echo "$BYTES" | tr -dc '[0-9]')
# compares integers (b/c no floats in bash)
while [ ${UNITS/.*} -ge ${MBYTES} ]
do
# floating point math via `bc`
UNITS=`echo "$PRECISION; $UNITS/${MBYTES}" | bc`
# increments counter for array pointer
((MAGNITUDE++))
done
echo "$BYTES bytes"
echo "$UNITS ${METRIC[$MAGNITUDE]}"
I don't really understand what the 'while' loop is doing, and I don't know
if zsh has 'floats' (which I assume is 'floating point'?).
So, given my limited knowledge here, I tried to implement the same thing in
zsh, and this is what I came up with:
BYTES="$@"
#MBYTES='1024'
MBYTES='1000'
PRECISION="scale=2"
if [ "$MBYTES" = "1000" ]
then
METRIC=('KB' 'MB' 'GB' 'TB' 'XB' 'PB')
else
METRIC=('KiB' 'MiB' 'GiB' 'TiB' 'XiB' 'PiB')
fi
for BYTES in "$@"
do
MAGNITUDE=0
UNITS=$(echo "$BYTES" | tr -dc '[0-9]')
if [[ "$UNITS" != "" ]]
then
while [ ${UNITS/.*} -ge $MBYTES ]
do
UNITS=$(echo "$PRECISION; $UNITS/$MBYTES" | bc)
((MAGNITUDE++))
done
echo "$UNITS ${METRIC[$MAGNITUDE]}"
fi
done
Does that look correct to you? Did I miss anything? It appears to work in
my (limited, so far) testing, but I'm worried that I'm overlooking
something.
Alternatively: is there another / easier way of doing this that I should
use instead? Some 'zmodload' or something?
Thanks for any pointers.
Tj
--
TJ Luoma
TJ @ MacStories
Personal Website: luo.ma (aka RhymesWithDiploma.com)
Twitter: @tjluoma
next reply other threads:[~2018-09-12 22:03 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-09-12 22:02 TJ Luoma [this message]
2018-09-12 22:38 ` TJ Luoma
2018-09-12 22:52 ` Bart Schaefer
2018-09-12 23:04 ` Daniel Shahaf
2018-09-12 23:08 ` Daniel Shahaf
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=CADjGqHtKCEc-cwrQXOtjVSrYE6U+tpP5aYWrfd9prrVmvOKrtg@mail.gmail.com \
--to=luomat@gmail.com \
--cc=zsh-users@zsh.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/zsh/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).