xfs
[Top] [All Lists]

[PATCH 1/2 v3] commit.rc: Add helper for math operation using bc

To: xfs@xxxxxxxxxxx
Subject: [PATCH 1/2 v3] commit.rc: Add helper for math operation using bc
From: Lukas Czerner <lczerner@xxxxxxxxxx>
Date: Fri, 23 Sep 2011 16:15:10 +0200
Cc: aelder@xxxxxxx, Lukas Czerner <lczerner@xxxxxxxxxx>
Sometimes using bash $(()) math might not be enough due to some
limitation (big numbers), so add helper using 'bc' program. For
now the results are only in perfect numbers (as in bash) since this is
all I need for now.

Signed-off-by: Lukas Czerner <lczerner@xxxxxxxxxx>
---
v3: Nothing has changed

 common.rc |   14 ++++++++++++++
 1 files changed, 14 insertions(+), 0 deletions(-)

diff --git a/common.rc b/common.rc
index 35f782b..b0e0c6a 100644
--- a/common.rc
+++ b/common.rc
@@ -20,6 +20,20 @@
 #  Mountain View, CA 94043, USA, or: http://www.sgi.com
 #-----------------------------------------------------------------------
 
+BC=$(which bc 2> /dev/null) || BC=
+
+_math() {
+       if [ $# -le 0 ]; then
+               return
+       fi
+       if [ "$BC" ]; then
+               result=$(LANG=C echo "scale=0; $@" | "$BC" -q 2> /dev/null)
+       else
+               result=$(($@))
+       fi
+       echo "$result"
+}
+
 dd()
 {
    if [ "$HOSTOS" == "Linux" ]
-- 
1.7.4.4

<Prev in Thread] Current Thread [Next in Thread>