LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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-08-2010, 10:32 AM   #1
januka
LQ Newbie
 
Registered: Sep 2010
Posts: 16

Rep: Reputation: 0
calling vars in a for loop in a shell script


Hi, I have the following question, any help is much appreciated;

# variable declaration
stt=10
enn=20
stp=1

# I want to do it in the following structure but not sure how to call
# the stt, enn and stp in a for loop

for (( i = $stt; i -le $enn; i=i + $stp ))
do
blah blah
done
 
Old 10-08-2010, 10:43 AM   #2
suprstar
Member
 
Registered: Aug 2010
Location: Atlanta
Distribution: ubuntu, debian
Posts: 142
Blog Entries: 2

Rep: Reputation: 21
How bout:

Code:
#!/bin/bash
stt=10
enn=20
stp=1
for i in `seq $stt $stp $enn`;
  do
     echo "iteration $i"
  done
Code:
man seq
for more details

Last edited by suprstar; 10-08-2010 at 10:46 AM.
 
Old 10-08-2010, 10:48 AM   #3
januka
LQ Newbie
 
Registered: Sep 2010
Posts: 16

Original Poster
Rep: Reputation: 0
nope, I have a strict variable selection. It MUST start from stt, end with enn and the increment should be stp. I could use it if there was a way to adjust the increment. Thanks suprstar!
 
Old 10-08-2010, 10:49 AM   #4
januka
LQ Newbie
 
Registered: Sep 2010
Posts: 16

Original Poster
Rep: Reputation: 0
ok, that sounds right. Thanks again !
 
Old 10-08-2010, 11:25 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
Version 4 of Bash has a new increment option for brace expansion. eval is needed when using variables, however, as they don't get substituted naturally.
Code:
for i in $(eval echo {$stt..$enn..$stp} ); do
	echo $i
done
 
Old 10-09-2010, 02:36 AM   #6
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,485

Rep: Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890
How about:
Code:
for (( i = stt;  i <= enn; i+=stp ))

Last edited by grail; 10-09-2010 at 02:47 AM.
 
Old 10-09-2010, 11:37 PM   #7
januka
LQ Newbie
 
Registered: Sep 2010
Posts: 16

Original Poster
Rep: Reputation: 0
David, didn't know that I had to use "echo" there, I tried it w/o it to no avail, no wonder. Grail, I tried that like
for (( i = $stt; i <= $enn; i=i+stp ))

it didn't work. I'll try it your way, it's an easy construct to remember. Thanks guys!
 
Old 10-10-2010, 12:41 AM   #8
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,485

Rep: Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890
You will notice the '$' are not needed but you should stay with one format or the other.
 
Old 10-10-2010, 12:55 PM   #9
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
Quote:
Originally Posted by januka View Post
David, didn't know that I had to use "echo" there, I tried it w/o it to no avail, no wonder.
Sorry, I guess I should've mentioned that too. I figured you'd be able to see it in my example. It's just another side-effect of using variables. eval needs to have an actual command of some sort to execute, apparently.

You could clean it up a little by moving the eval into a separate variable...
Code:
sequence=$( eval echo {$stt..$enn..$stp} )

for i in $sequence; do
	echo $i
done
But really, in this situation the pattern grail provided is much more appropriate. Save the brace expansion for something that doesn't need to use variables.
 
Old 10-10-2010, 11:41 PM   #10
januka
LQ Newbie
 
Registered: Sep 2010
Posts: 16

Original Poster
Rep: Reputation: 0
David, that's interesting, haven't seen it like that before, Thanks again guys !
 
  


Reply


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Calling a bash shell script from within another script dedman Linux - Software 7 04-24-2010 08:53 PM
how to exit out of the calling Shell Script harimac Linux - Newbie 3 08-19-2009 01:49 AM
Calling PHP5 from a shell script Dafydd Programming 2 10-31-2008 07:31 PM
Directory listing - Calling shell script from a CGI script seran Programming 6 08-11-2005 11:08 PM
shell script backticks and vars? SheldonPlankton Programming 4 07-21-2004 11:29 AM


All times are GMT -5. The time now is 02:19 PM.

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