Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Thu 24 Jul 2014, 21:58
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
include variable in bash loop [Solved]
Post_new_topic   Reply_to_topic View_previous_topic :: View_next_topic
Page 1 of 1 Posts_count  
Author Message
Lobster
Official Crustacean


Joined: 04 May 2005
Posts: 15117
Location: Paradox Realm

PostPosted: Thu 05 Aug 2010, 00:03    Post_subject:  include variable in bash loop [Solved]  

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

How?
this what I have so far

Code:
repetitions=8

for g in {repetitions..1}
  do
              stuff to do here
  done

_________________
Puppy WIKI

Edited_time_total
Back to top
View user's profile Send_private_message Visit_website 
potong

Joined: 06 Mar 2009
Posts: 88

PostPosted: Thu 05 Aug 2010, 00:16    Post_subject:  

Lobster:
Here are two ways to do it:
Code:
# 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!
Back to top
View user's profile Send_private_message 
Lobster
Official Crustacean


Joined: 04 May 2005
Posts: 15117
Location: Paradox Realm

PostPosted: Thu 05 Aug 2010, 14:13    Post_subject:  

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:
for g in {repetitions..1}

to run like so
Code:
for g in {22..1}


Is that what your code does?
If so I do not understand it

_________________
Puppy WIKI
Back to top
View user's profile Send_private_message Visit_website 
seaside

Joined: 11 Apr 2007
Posts: 886

PostPosted: Thu 05 Aug 2010, 15:42    Post_subject:  

Lobster,

Perhaps this might help.

Code:
 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. Very Happy

Regards,
s
Back to top
View user's profile Send_private_message 
Lobster
Official Crustacean


Joined: 04 May 2005
Posts: 15117
Location: Paradox Realm

PostPosted: Thu 05 Aug 2010, 18:30    Post_subject:  

Seaside
Understood
Works
Thanks
Appreciated Cool

Thanks guys

Puppy Linux
Penguinated Assistance available

_________________
Puppy WIKI
Back to top
View user's profile Send_private_message Visit_website 
potong

Joined: 06 Mar 2009
Posts: 88

PostPosted: Fri 06 Aug 2010, 00:17    Post_subject:  

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:
# 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:
# 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:
# 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:
# 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:
# echo $n
21
however in bash arithmetic ((...)) and array subscripts [...] you don't need to dereference so:
Code:
# (( answer=n+repetitions ))
# echo $answer
42
of course you can dereference the result of arithmetic by prepending a '$' to it:
Code:
# 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).
Back to top
View user's profile Send_private_message 
Lobster
Official Crustacean


Joined: 04 May 2005
Posts: 15117
Location: Paradox Realm

PostPosted: Fri 06 Aug 2010, 02:10    Post_subject:  

Thanks potong

I have the code I need now
Appreciate the help Smile

Used it here
http://www.murga-linux.com/puppy/viewtopic.php?p=438198#438198

_________________
Puppy WIKI
Back to top
View user's profile Send_private_message Visit_website 
Bruce B


Joined: 18 May 2005
Posts: 11080
Location: The Peoples Republic of California

PostPosted: Fri 06 Aug 2010, 23:17    Post_subject:  

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:
#!/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.

_________________
New! Puppy Linux Links Page
Back to top
View user's profile Send_private_message 
Display_posts:   Sort by:   
Page 1 of 1 Posts_count  
Post_new_topic   Reply_to_topic View_previous_topic :: View_next_topic
 Forum index » Off-Topic Area » Programming
Jump to:  

Rules_post_cannot
Rules_reply_cannot
Rules_edit_cannot
Rules_delete_cannot
Rules_vote_cannot
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.0607s ][ Queries: 11 (0.0030s) ][ GZIP on ]