From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: zsh-users-return-23626-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=-1.1 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE 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 229adb50 for ; Wed, 12 Sep 2018 23:09:01 +0000 (UTC) Received: (qmail 1872 invoked by alias); 12 Sep 2018 23:08:49 -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: 23626 Received: (qmail 20969 invoked by uid 1010); 12 Sep 2018 23:08:49 -0000 X-Qmail-Scanner-Diagnostics: from out1-smtp.messagingengine.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(66.111.4.25):SA:0(-2.6/5.0):. Processed in 1.172552 secs); 12 Sep 2018 23:08:49 -0000 X-Envelope-From: d.s@daniel.shahaf.name X-Qmail-Scanner-Mime-Attachments: | X-Qmail-Scanner-Zip-Files: | DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= daniel.shahaf.name; h=cc:content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-sender:x-me-sender:x-sasl-enc; s=fm1; bh=C2WLxR PmubP0b50zsu7kFgVuyeXr5eOtgNjfnsR3uZU=; b=nDHmozQdH0RQtEXztMspCu uqJSmO88v17NMvtjmuNM03QOwyjup8yLx41cFzud0DItNzkboUKTUhfJVfQX4r5F BBPSyUX8AwEeNVqygv3TvSFZyLynM+c3kUBjX+OTHJIb9H68tV/DIC4jLdaDlTD+ dZCFOXma82DbHmfXyEtLjjB68KQzS98wWsEDkGAt9NtCtLCHenmH7KMpGnmDt/CW 8qRMUUPqccn4x7xnJ+xVCHb3JiJQbX/6y+1Wdi/oNJowG6jO1Ju/MIyeTbrIve6k wD6CuaGlADR4tOrWV2mTasn2EPRI19CFlHPE6mcb6L+yDZcFmB6J+tjJ2SAXabWA == DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-sender:x-me-sender:x-sasl-enc; s=fm3; bh=C2WLxR PmubP0b50zsu7kFgVuyeXr5eOtgNjfnsR3uZU=; b=RUmtgfNziKZ9DkWBZrBwur LjVt8nhIzvJ8onvzIQvFjav8eEg1/sssJYX5z+4whx6EUoowfOoh7qFM+JQS6LRz v7EnhH+nVXu36oyCYoV3piDiV45DftAr9YjDz/q5EfiZnSVNioW4tz+YJBe94+4H xXiYufa9n30jTSzQ63mqoW7UtW6nn7tAm8yz7h1wLTnmzlzAv0JjVbyOhMaURq0g plcUVUFJl8ia8ZtgxTX03GWqwfrIUS9proY/BKHvCuUbd38qJ14Ruv/d5F4RXxeS Y3a6QvMP7HvW+cd72i4zXeLbNEZdO7Fy6Xh9FhIF8owemZWWWMEZDiTqvg/6qVAA == X-ME-Proxy: X-ME-Sender: Date: Wed, 12 Sep 2018 23:08:42 +0000 From: Daniel Shahaf To: TJ Luoma Cc: Zsh-Users List Subject: Re: Converting bash script to zsh for converting bytes to 'readable' Message-ID: <20180912230842.db66mgh3px2rsbru@tarpaulin.shahaf.local2> References: <20180912230445.fpfihe7htuqkfbed@tarpaulin.shahaf.local2> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20180912230445.fpfihe7htuqkfbed@tarpaulin.shahaf.local2> User-Agent: NeoMutt/20170113 (1.7.2) Daniel Shahaf wrote on Wed, Sep 12, 2018 at 23:04:45 +0000: > 1 zmodload zsh/mathfunc > 2 f() { > 3 setopt localoptions ksharrays > 4 integer i=$(( log2( $1 )/10 )) > 5 local -a SI=( '' 'Ki' 'Mi' 'Gi' ) > 6 printf '%5.2g %s\n' "$(( ($1)*1.0 / (1<<(10*i)) ))" "${SI[i]}B" > 7 } A subtle point here: in an arithmetic expression, using raw variable names behave as one'd expect, but using variable expansions does not. Compare: % s='1+1' % echo $(( s * 2 )) 4 % echo $(( $s * 2 )) 3 % That's why I put parentheses around the «$1» in the math expression: that ensures consistent parsing even if $1 is an expression (as in «f "1024+1024"»). Cheers, Daniel (It's exactly the same issue as with C macros)