From owner-freebsd-questions@FreeBSD.ORG Wed Aug 31 18:42:10 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 4864D16A41F for ; Wed, 31 Aug 2005 18:42:10 +0000 (GMT) (envelope-from dan@dan.emsphone.com) Received: from dan.emsphone.com (dan.emsphone.com [199.67.51.101]) by mx1.FreeBSD.org (Postfix) with ESMTP id 67A7A43D6B for ; Wed, 31 Aug 2005 18:42:05 +0000 (GMT) (envelope-from dan@dan.emsphone.com) Received: (from dan@localhost) by dan.emsphone.com (8.13.1/8.13.3) id j7VIg5Hu020328; Wed, 31 Aug 2005 13:42:05 -0500 (CDT) (envelope-from dan) Date: Wed, 31 Aug 2005 13:42:05 -0500 From: Dan Nelson To: Gary Kline Message-ID: <20050831184205.GC16354@dan.emsphone.com> References: <20050831181545.GA57907@thought.org> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20050831181545.GA57907@thought.org> X-OS: FreeBSD 5.4-STABLE X-message-flag: Outlook Error User-Agent: Mutt/1.5.9i Cc: 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 18:42:10 -0000 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) )) which works since sh's arithmetic evaluator is integer-only. zsh has the % modulo operator, so xmod=$(( x % n )) . -- Dan Nelson dnelson@allantgroup.com