LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Loops in Bash shell or variable math... (http://www.linuxquestions.org/questions/programming-9/loops-in-bash-shell-or-variable-math-841032/)

edpatterson 10-28-2010 04:28 PM

Loops in Bash shell or variable math...
 
I am trying to map the coordinates of a grid.

Code:

R=7    # number of rows (lines)
C=6    # number of columns
X=200  # initial horizontal location
Y=100  # initial vertical location
XDIS=25 # horizontal distance cell to cell
YDIS=26 # vertical distance cell to cell
XORG=$X # used to begin each row
for i in $(eval echo {1..$R}; do  # works, based upon other help
  for j in $(eval echo {1..$C}; do # works, based upon other help
    echo "$Y:$X"
    $X=$X+$XDIS # This is what is blowing up
  done
  $X=$XORG      # I am assuming this will not work either
  $Y=$Y+YDIS    # ""
done


impert 10-28-2010 04:41 PM

Quote:

$X=$X+$XDIS # This is what is blowing up
Try:
X=$(($X+$XDIS))
1 The assignment should not start with the $
2 Arithmetic is done within $(( ))


Quote:

$X=$XORG # I am assuming this will not work either
$Y=$Y+YDIS # ""
I'd say those are fair assumptions.

I'm only learning myself, so if I've led you astray, my apologies.
It's a good idea to try snippets of code in a terminal.

edpatterson 10-28-2010 04:55 PM

Thanks! That did it.

Thanks!

Ed

GrapefruiTgirl 10-28-2010 05:01 PM

Also, for the sake of completeness, you don't need the $ inside the $(()) like below:
Code:

X=$((a+b)
That will work just as well. :)

grail 10-29-2010 01:09 AM

Or maybe even:
Code:

for ((i=1; i <= $R; i++))
  for ((j=1; j <= $C; j++))
    echo "$Y:$X"
    ((X+=XDIS))
  done

  ((X=XORG))  # changed this one only to keep all numeric changes the same
  ((Y+=YDIS))
done



All times are GMT -5. The time now is 11:21 PM.