Doing a modulo in /bin/sh??
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? tia, guys, gary #/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 -- Gary Kline [EMAIL PROTECTED] www.thought.org Public service Unix ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "[EMAIL PROTECTED]"
Re: Doing a modulo in /bin/sh??
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? tia, guys, gary #/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 Take a look at 'expr' [EMAIL PROTECTED]:~ % expr 1 % 2 1 [EMAIL PROTECTED]:~ % expr 2 % 2 0 ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "[EMAIL PROTECTED]"
Re: Doing a modulo in /bin/sh??
On Wed, Aug 31, 2005 at 11:18:57AM -0700, Philip Hallstrom wrote: > > 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? > > > > tia, guys, > > > > gary > > > >#/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 > > Take a look at 'expr' > > [EMAIL PROTECTED]:~ > % expr 1 % 2 > 1 > [EMAIL PROTECTED]:~ > % expr 2 % 2 > 0 Thankee, sir; I forgot that with ash/sh/<&c>, y'gotta use expr; and inside backticks in cases like this one. gary #/bin/sh w=$(date +%U) echo "w is $w"; even=`expr ${w} % 2`; echo "even is $even"; if [ $even -eq 0 ] then echo "week is even"; else echo "week is odd"; fi -- Gary Kline [EMAIL PROTECTED] www.thought.org Public service Unix ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "[EMAIL PROTECTED]"
Re: Doing a modulo in /bin/sh??
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 [EMAIL PROTECTED] ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "[EMAIL PROTECTED]"
Re: Doing a modulo in /bin/sh??
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 [EMAIL PROTECTED] ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "[EMAIL PROTECTED]"
Re: Doing a modulo in /bin/sh??
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 [EMAIL PROTECTED] www.thought.org Public service Unix ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "[EMAIL PROTECTED]"