View Single Post
  #2   (View Single Post)  
Old 18th November 2008
J65nko J65nko is offline
Administrator
 
Join Date: May 2008
Location: Budel - the Netherlands
Posts: 3,503
Default

It is a operator precedence issue. In the OpenBSD ksh you see something similar
Code:
$ cat round
game=1

for x in 1 2 3 4 5 ;  do
   echo Game nr: $game Player: $(( game++ % 2 + 1 ))
done

echo ----------

game=1

for x in 1 2 3 4 5 ;  do
   echo Game nr: $game Player: $(( ++game % 2 + 1 ))
done

echo ----------

game=1

for x in 1 2 3 4 5 ;  do
   echo Game nr: $game Player: $(( game % 2 + 1 ))
   game=$((++game))
done
A run produces:
Code:
$ sh round

Game nr: 1 Player: 2
Game nr: 2 Player: 1
Game nr: 3 Player: 2
Game nr: 4 Player: 1
Game nr: 5 Player: 2
----------
Game nr: 1 Player: 1
Game nr: 2 Player: 2
Game nr: 3 Player: 1
Game nr: 4 Player: 2
Game nr: 5 Player: 1
----------
Game nr: 1 Player: 2
Game nr: 2 Player: 1
Game nr: 3 Player: 2
Game nr: 4 Player: 1
Game nr: 5 Player: 2
Here only the second calculation "$(( ++game % 2 + 1 )) produces the desired result isn't it? How about trying this pre-increment variation
__________________
You don't need to be a genius to debug a pf.conf firewall ruleset, you just need the guts to run tcpdump
Reply With Quote