From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: zsh-users-return-23622-ml=inbox.vuxu.org@zsh.org X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE, T_DKIM_INVALID autolearn=ham autolearn_force=no version=3.4.1 Received: from primenet.com.au (ns1.primenet.com.au [203.24.36.2]) by inbox.vuxu.org (OpenSMTPD) with ESMTP id e395686d for ; Wed, 12 Sep 2018 22:03:29 +0000 (UTC) Received: (qmail 5470 invoked by alias); 12 Sep 2018 22:03:10 -0000 Mailing-List: contact zsh-users-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Users List List-Post: List-Help: List-Unsubscribe: X-Seq: 23622 Received: (qmail 24819 invoked by uid 1010); 12 Sep 2018 22:03:10 -0000 X-Qmail-Scanner-Diagnostics: from mail-wm0-f44.google.com by f.primenet.com.au (envelope-from , uid 7791) with qmail-scanner-2.11 (clamdscan: 0.99.2/21882. spamassassin: 3.4.1. Clear:RC:0(74.125.82.44):SA:0(-1.9/5.0):. Processed in 2.12324 secs); 12 Sep 2018 22:03:10 -0000 X-Envelope-From: luomat@gmail.com X-Qmail-Scanner-Mime-Attachments: | X-Qmail-Scanner-Zip-Files: | DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:from:date:message-id:subject:to; bh=XDRiV+r7zPMfxVJ8cmi+R1rqwoMaYnAYJgAxWmKQkOU=; b=J9tPbL6SUQrvrnG2tcpsX9bJ+5BgpoQM6DpMWjNU/qdSmpNU6jH/+KAqvIbJ39RXI7 YMsfQag/A467T9/PAzLroKpG4xnaH3whD93tGSV4LPZFqYaGck7maE66DUEECAco4Fo5 LXBoqgyUIWUyarS4GdqLkPmO/phVxOb3ImNRWkJDvqPMejosv4B+FMsGgL/2HurwyFxx j+STDdt3Y21N81R2K9Mt0baytmsm4seeePuZrzBjEqKealTe29/B11uCjLX7tgIzGU8m Fky8PlJz4uYjOPfJSExmpoTBEm8ysYdNmi0qvVfdSdvqEBI0BkZHEM1KhsbCb77RUtzm avGg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=XDRiV+r7zPMfxVJ8cmi+R1rqwoMaYnAYJgAxWmKQkOU=; b=EQxLIGMPad3WQvJMaygFOOZLrZRpbrKsKRLtrQHZFM25U2tzbGVULhAsF2D2OpsdZF EUiYVcRTLl1o0FdLjbJVBCAaHoN/Sgk/mR6kG6n4BwrpZbJvN5iGvu55bn1uCRebHrJj um6DcGl9BDf6yzxoF231Xvr+oiTuYxUhr5SN0CtGNUMNgmJblrC8IVOcy5s9n4I18g6J FVUAQ78ThzKw0m345I+FD2v291agLPaZNFy1UeijyKiHSItPhHiNWwGxpxyNXvfIdBig DhOuhV5aAfLQ7YGNEg7/Uc7IM78PWhO9mJufP6kZ5tE77a1jSWAQSk79DqY/7fY3jz9m EVfQ== X-Gm-Message-State: APzg51AnmjfCOTr0GN/8e0B+xpJcS8abda/c2ttCt9Ahszdl48HPrpkt BnQ84vC4Booo5+3XuvY5IL3qLWfR21Xk5PO58uyljiZJ X-Google-Smtp-Source: ANB0VdaBBonN3ScBy96dahcmmquVN08/G33BoFGqW0wyEbsn96lmAVXRjTA69wfYcDlGSgWWj82N2UHrqQg7G9S6x34= X-Received: by 2002:a1c:1182:: with SMTP id 124-v6mr3141289wmr.75.1536789783572; Wed, 12 Sep 2018 15:03:03 -0700 (PDT) MIME-Version: 1.0 From: TJ Luoma Date: Wed, 12 Sep 2018 18:02:26 -0400 Message-ID: Subject: Converting bash script to zsh for converting bytes to 'readable' To: Zsh-Users List Content-Type: multipart/alternative; boundary="00000000000024f41a0575b3c168" --00000000000024f41a0575b3c168 Content-Type: text/plain; charset="UTF-8" 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 --00000000000024f41a0575b3c168--