Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 31 Aug 2005 13:49:14 -0500
From:      Dan Nelson <dnelson@allantgroup.com>
To:        Gary Kline <kline@tao.thought.org>
Cc:        FreeBSD Mailing List <freebsd-questions@freebsd.org>
Subject:   Re: Doing a modulo in /bin/sh??
Message-ID:  <20050831184914.GD16354@dan.emsphone.com>
In-Reply-To: <20050831184205.GC16354@dan.emsphone.com>
References:  <20050831181545.GA57907@thought.org> <20050831184205.GC16354@dan.emsphone.com>

next in thread | previous in thread | raw e-mail | index | archive | help
In the last episode (Aug 31), Dan Nelson said:
> In the last episode (Aug 31), Gary Kline said:
> > I can grab the results of "w=$date+%U)"; in C an use the modulo
> > operator; is there a way to do this is /bin/sh?  ot zsh?
> > 
> > #/bin/sh
> > w=$(date +%U)
> > echo "w is $w";
> > (even=$(w % 2 ));       ## flubs.
> > echo "even is $even";       ## flubs.
> > 
> > if [ $even -eq 0 ]       ## flubs, obv'ly.
> > then
> >         echo "week is even";
> > else
> >         echo "week is odd";
> > fi
> 
> zsh has the % modulo operator, so xmod=$(( x % n )) .

Silly me, I forgot to read the source to /bin/sh's arithmetic code.  It
knows about % too, so $(( x % n )) will work anywhere.

-- 
	Dan Nelson
	dnelson@allantgroup.com



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20050831184914.GD16354>