LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 08-22-2012, 05:56 PM   #1
hachteja
LQ Newbie
 
Registered: Aug 2012
Posts: 2

Rep: Reputation: Disabled
Iteration dependent variables in a loop


I'm writing a bash script where I will have a different amount of variables depending on another parameter.

So if parameter a=5, I'll need var1-5, but if a=3 I only need var1-3.

My inclination is to set this up in a for loop, and have the loop go from 1-5, each time setting the value of var$a. However, I have found that bash doesn't like it when you use a variable on the left side of the equals sign.

I am fairly new to bash, but I can't imagine this is impossible. I'm not so familiar with arrays in bash, and feel like that will probably end up being the answer.

Any thoughts or advice would be much appreciated.

Thanks so much.

-JAH
 
Old 08-22-2012, 06:05 PM   #2
byannoni
Member
 
Registered: Aug 2012
Location: /home/byannoni
Distribution: Arch
Posts: 128

Rep: Reputation: 36
You can do this:
Code:
i=1
eval $(echo "test$i")=success
echo test1

Last edited by byannoni; 08-22-2012 at 06:06 PM.
 
1 members found this post helpful.
Old 08-22-2012, 06:45 PM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,362

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
You can use eval, but its not generally recommended.
I'd probably store all the values for 'var1->however many' in an array, and then just pull them out depending on the setting of the ctrl var.
See http://tldp.org/LDP/abs/html/arrays.html

and generally bookmark these
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/
 
1 members found this post helpful.
Old 08-22-2012, 09:04 PM   #4
hachteja
LQ Newbie
 
Registered: Aug 2012
Posts: 2

Original Poster
Rep: Reputation: Disabled
Thanks guys
 
  


Reply



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
BASH: restarts the iteration from the beginning of the loop cristalp Programming 9 10-25-2012 10:27 AM
[SOLVED] while loop not printing message for each iteration in c language UnixCube Linux - Newbie 3 04-29-2012 07:41 PM
[SOLVED] bash for loop iteration question. ncalsmitty1369 Programming 4 02-05-2010 05:50 PM
python: can I see which iteration I'm on in a loop? BrianK Programming 2 08-27-2008 09:01 PM
Loop iteration in Linux scripting. 151803 Linux - Newbie 3 03-19-2007 06:36 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 04:35 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