From owner-freebsd-questions@FreeBSD.ORG Wed Aug 31 21:48:34 2005 Return-Path: X-Original-To: freebsd-questions@freebsd.org 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 0F50D16A41F for ; Wed, 31 Aug 2005 21:48:34 +0000 (GMT) (envelope-from kline@tao.thought.org) Received: from tao.thought.org (dsl231-043-140.sea1.dsl.speakeasy.net [216.231.43.140]) by mx1.FreeBSD.org (Postfix) with ESMTP id 4752D43D48 for ; Wed, 31 Aug 2005 21:48:31 +0000 (GMT) (envelope-from kline@tao.thought.org) Received: from tao.thought.org (localhost [127.0.0.1]) by tao.thought.org (8.13.1/8.13.1) with ESMTP id j7VLmIOl060198; Wed, 31 Aug 2005 14:48:18 -0700 (PDT) (envelope-from kline@tao.thought.org) Received: (from kline@localhost) by tao.thought.org (8.13.1/8.13.1/Submit) id j7VLmIKf060197; Wed, 31 Aug 2005 14:48:18 -0700 (PDT) (envelope-from kline) Date: Wed, 31 Aug 2005 14:48:17 -0700 From: Gary Kline To: Dan Nelson Message-ID: <20050831214817.GA60141@thought.org> References: <20050831181545.GA57907@thought.org> <20050831184205.GC16354@dan.emsphone.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20050831184205.GC16354@dan.emsphone.com> User-Agent: Mutt/1.4.2.1i X-Organization: Thought Unlimited. Public service Unix since 1986. X-Of_Interest: Observing 19 years of service to the Unix community Cc: Gary Kline , FreeBSD Mailing List Subject: Re: Doing a modulo in /bin/sh?? X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 31 Aug 2005 21:48:34 -0000 On Wed, Aug 31, 2005 at 01:42:05PM -0500, Dan Nelson wrote: > 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 > > For the simple even/odd case, you can AND with 1: > > even=$(( w & 1 )) > > For the general case: > > xmodn=$(( x - ((x / n) * n) )) I didn't think of the first cast, but yep; the general is seriously sharp in my book; got to salt this away:-) Thanks. > > which works since sh's arithmetic evaluator is integer-only. > > zsh has the % modulo operator, so xmod=$(( x % n )) . > I knew ksh/zsh/bash(?) have it; forgot about `expr`, tho. gary -- Gary Kline kline@thought.org www.thought.org Public service Unix