LinuxQuestions.org
Help answer threads with 0 replies.
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
 
LinkBack Search this Thread
Old 10-28-2010, 04:28 PM   #1
edpatterson
LQ Newbie
 
Registered: Aug 2003
Location: Florida
Distribution: Ubuntu
Posts: 12

Rep: Reputation: 0
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: 275

Rep: Reputation: 53
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: 12

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

Thanks!

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

Rep: Reputation: 542Reputation: 542Reputation: 542Reputation: 542Reputation: 542Reputation: 542
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
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,202

Rep: Reputation: 1796Reputation: 1796Reputation: 1796Reputation: 1796Reputation: 1796Reputation: 1796Reputation: 1796Reputation: 1796Reputation: 1796Reputation: 1796Reputation: 1796
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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are On
Refbacks are 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


All times are GMT -5. The time now is 08:08 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration