include variable in bash loop [Solved]

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
User avatar
Lobster
Official Crustacean
Posts: 15522
Joined: Wed 04 May 2005, 06:06
Location: Paradox Realm
Contact:

include variable in bash loop [Solved]

#1 Post by Lobster »

Trying to set a variable - repetitions
and then include that in a loop

How?
this what I have so far

Code: Select all

repetitions=8

for g in {repetitions..1}
  do
              stuff to do here
  done
Last edited by Lobster on Fri 06 Aug 2010, 19:05, edited 1 time in total.
Puppy Raspup 8.2Final 8)
Puppy Links Page http://www.smokey01.com/bruceb/puppy.html :D

potong
Posts: 88
Joined: Fri 06 Mar 2009, 04:01

#2 Post by potong »

Lobster:
Here are two ways to do it:

Code: Select all

# echo {1..8}
1 2 3 4 5 6 7 8
# i=8
# echo {1..$i}
{1..8}
# eval echo {1..$i}
1 2 3 4 5 6 7 8
# for j in $(eval echo {1..$i}) { echo j=$j; }
bash: syntax error near unexpected token `}'
# for j in $(eval echo {1..$i});do echo j=$j; done
j=1
j=2
j=3
j=4
j=5
j=6
j=7
j=8
# for ((j=0;j<i;j++)){ echo j=$j; }
j=0
j=1
j=2
j=3
j=4
j=5
j=6
j=7
HTH

Potong

p.s. {8..1} works too!

User avatar
Lobster
Official Crustacean
Posts: 15522
Joined: Wed 04 May 2005, 06:06
Location: Paradox Realm
Contact:

#3 Post by Lobster »

Thanks for reply but I am not sure I have stated the problem correctly

repetitions=8
is where the variable is declared
if I change it to 22
I want

Code: Select all

for g in {repetitions..1}
to run like so

Code: Select all

for g in {22..1}
Is that what your code does?
If so I do not understand it
Puppy Raspup 8.2Final 8)
Puppy Links Page http://www.smokey01.com/bruceb/puppy.html :D

seaside
Posts: 934
Joined: Thu 12 Apr 2007, 00:19

#4 Post by seaside »

Lobster,

Perhaps this might help.

Code: Select all

 n=21 #no of repetitions
for g in $(eval echo {1..$n}); do    echo "Hello $g times"; done
The variable "n" needs to be substituted first, the expression then evaluated and expanded so that the expression becomes "for g in 1 2 3 4 .......21".

I don't know if that explanation satisfies, but then again very few products do except maybe beer. :D

Regards,
s

User avatar
Lobster
Official Crustacean
Posts: 15522
Joined: Wed 04 May 2005, 06:06
Location: Paradox Realm
Contact:

#5 Post by Lobster »

Seaside
Understood
Works
Thanks
Appreciated 8)

Thanks guys

Puppy Linux
Penguinated Assistance available
Puppy Raspup 8.2Final 8)
Puppy Links Page http://www.smokey01.com/bruceb/puppy.html :D

potong
Posts: 88
Joined: Fri 06 Mar 2009, 04:01

#6 Post by potong »

Lobster:

Sorry my reply wasn't clear.

Try opening a terminal session and then copy & paste the code lines beginning with a '#' into it (exclude the '#' itself; this is a prompt and not part of the command).

On reflection you want a 'count down' effect so:

Code: Select all

# for g in $(eval echo {$repetitions..1}); do echo g=$g; done
g=21
g=20
...
g=2
g=1
should do the trick
or

Code: Select all

# for ((g=repetitions; g > 0; g-- )) { echo g=$g; }
g=21
g=20
...
g=2
g=1
a third way at the expense of another process is:

Code: Select all

# for g in $(seq 21 -1 1); do echo g=$g; done
g=21
g=20
...
g=2
g=1
N.B bash assigns a value like so:

Code: Select all

# n=21
to get the value of the variable n you need to place a '$' in front of the variable identifier to dereference it:

Code: Select all

# echo $n
21
however in bash arithmetic ((...)) and array subscripts [...] you don't need to dereference so:

Code: Select all

# (( answer=n+repetitions ))
# echo $answer
42
of course you can dereference the result of arithmetic by prepending a '$' to it:

Code: Select all

# echo $(( n + repetitions ))
42
HTH

Potong

p.s. copy by selecting (hold down left click and drag) in the browser, paste in rxvt by middle click (click the scroll wheel or if you only have 2 buttons click both at the same time).

User avatar
Lobster
Official Crustacean
Posts: 15522
Joined: Wed 04 May 2005, 06:06
Location: Paradox Realm
Contact:

#7 Post by Lobster »

Thanks potong

I have the code I need now
Appreciate the help :)

Used it here
http://www.murga-linux.com/puppy/viewto ... 198#438198
Puppy Raspup 8.2Final 8)
Puppy Links Page http://www.smokey01.com/bruceb/puppy.html :D

Bruce B

#8 Post by Bruce B »

For eight controlled loops using a C style for loop

To increase or decrease the number of loops modify
the i<8 portion of the statement.

Code: Select all

#!/bin/bash

for ((i=0;i<8;i++))
do
echo $i
done
PS - sorry it looks like potong already showed an example of this type of control above, but I didn't catch it first time around.

Post Reply