LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-28-2010, 04:28 PM   #1
edpatterson
LQ Newbie
 
Registered: Aug 2003
Location: Florida
Distribution: Ubuntu
Posts: 13

Rep: Reputation: 1
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
 
Old 10-28-2010, 04:41 PM   #2
impert
Member
 
Registered: Feb 2009
Posts: 282

Rep: Reputation: 54
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.
 
1 members found this post helpful.
Old 10-28-2010, 04:55 PM   #3
edpatterson
LQ Newbie
 
Registered: Aug 2003
Location: Florida
Distribution: Ubuntu
Posts: 13

Original Poster
Rep: Reputation: 1
Thanks! That did it.

Thanks!

Ed
 
Old 10-28-2010, 05:01 PM   #4
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
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.
 
Old 10-29-2010, 01:09 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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
 
1 members found this post helpful.
  


Reply

Tags
bash, loops, variables



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
In BASH shell, how do I export a variable from a script. MichaelG67 Linux - Software 2 06-24-2010 01:03 PM
csh Shell Script: Getting wc command output to variable to do math on it, how? vxc69 Programming 5 05-04-2009 04:31 PM
Bash/Shell: Problem using a variable in a script redvivi Linux - General 8 08-21-2008 02:02 PM
how to export a bash shell variable before init.d process start pnsidhu Linux - Newbie 1 06-04-2008 08:21 AM
Bash Shell Scripting Dynamic Variable naming question ZuG Programming 2 02-07-2007 02:39 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration