From owner-freebsd-questions@FreeBSD.ORG Fri Oct 1 08:54:50 2004 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id AEDFD16A4CF for ; Fri, 1 Oct 2004 08:54:50 +0000 (GMT) Received: from mx1.mail.ru (mx1.mail.ru [194.67.23.121]) by mx1.FreeBSD.org (Postfix) with ESMTP id 6908743D49 for ; Fri, 1 Oct 2004 08:54:50 +0000 (GMT) (envelope-from infofarmer@mail.ru) Received: from [83.237.13.52] (port=5452 helo=SATPC) by mx1.mail.ru with smtp id 1CDJBg-0000Ww-00 for freebsd-questions@freebsd.org; Fri, 01 Oct 2004 12:54:48 +0400 Message-ID: <001501c4a794$81d031c0$4611a8c0@SATPC> From: "Andrew" Cc: References: <000601c4a720$99264270$4611a8c0@SATPC> <20040930194001.GD22530@dan.emsphone.com> <004901c4a729$61fc7810$4611a8c0@SATPC> <20040930204808.GE22530@dan.emsphone.com> Date: Fri, 1 Oct 2004 12:56:14 +0400 MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 6.00.2800.1437 X-MIMEOLE: Produced By Microsoft MimeOLE V6.00.2800.1441 X-Spam: Not detected Subject: Re: 64-bit arithmetic in scripts? X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 01 Oct 2004 08:54:50 -0000 Dan Nelson has kindly explained everything: > In the last episode (Oct 01), Andrew said: > > Thanks! I haven't thought about using expr. > > > > How come that my expr(1) manpage has nothing to say about -e option? > > In fact my expr(1) does not accept it. I have FreeBSD 4.10. I've just > > looked into a current manpage from www.freebsd.org, and it says > > something about 4.x compatibility. > > > > What is the best way to go if I need to write scripts now, but I'm > > planning to switch to 5.x later? Can I upgrade expr(1) now? If not, > > what should I do? > > In 4.x, expr does 64-bit math by default. Apparently POSIX requires > that expr use whatever the systems' "signed long" size is, so the > default was changed for 5.x, and -e was added to get the old behaviour. > > If you want your script to work on both, you'll have to do a feature > test. I started out just testing expr and expr -e, but it sort of > grew... The following script will check the shell's math, two ways of > calling expr, and finally fall back on calling bc. As long as you just > use the basic math operators, quote your "*"'s, and put spaces between > everything, all the methods should be compatible, and your script will > work on any bourne-compatible shell :) > > #! /bin/sh > > if [ x$(( 65536 * 65536 )) = "x4294967296" ] ; then > shellarith() { echo $(( $@ )) ; } > MATH="shellarith" > else > if [ x`expr 65536 "*" 65536` = "x4294967296" ] ; then > MATH="expr" > else > if [ x`expr -e 65536 "*" 65536 2>/dev/null` = x"4294967296" ] ; then > MATH="expr -e" > else > if [ x`echo 65536 "*" 65536 | bc` = "x4294967296" ] ; then > bcfunc() { echo "$@" | bc ; } > MATH="bcfunc" > else > echo "Can't do 64-bit math noway nohow" > fi > fi > fi > fi > > echo "Using $MATH" > bigval=`$MATH 65536 "*" 65536` > echo $bigval Thank you very much! I think, I'll just assign MATH="expr" and change it to something else when I need it. Best regards, Andrew P.