LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Red Hat
User Name
Password
Red Hat This forum is for the discussion of Red Hat Linux.

Notices



Reply
 
Search this Thread
Old 12-07-2011, 02:27 PM   #1
rockf1bull
LQ Newbie
 
Registered: Feb 2011
Posts: 19

Rep: Reputation: 0
Using multiple values for single variable in a loop


Hello Guys,

I have a small loop problem as below.

I have 3 different values to be used while running the same script -

va1="some-value1"
va2="some-value2"
va3="some-value3"

Now I want to use these three variable values to be used for running the same command, like -

while (i=0,i<=3,i++)
do
bin/java -s (run something with $var);
done


Now I want $var taking the value of var1, var2 and var3 each time it runs,

so can someone please tell me how do we achieve the above?

I tried doing -

for $1 $2 $3

do
case 1
case 2
case 3
done


OR

while read a b c
do
<code assumed to have loop iteration>
done <<< $(command)


But it isnt working as expected... Would really appreciate your help on this.


Thanks,
Brian
 
Old 12-07-2011, 02:53 PM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
Very simply, don't use separate variables. Whenever you have lists of related entries, you should use an array.

http://mywiki.wooledge.org/BashGuide/Arrays
http://mywiki.wooledge.org/BashFAQ/005

Code:
va[0]="some-value1"
va[1]="some-value2"
va[2]="some-value3"

for (i=0;i<=2;i++); do
	bin/java -s "${va[i]}"
done
[P.S. The syntax for your loop was all wrong. A while loop does not use a c-style counter. The correct form to use is shown above. http://mywiki.wooledge.org/BashGuide...ndConditionals ]


(This is of course assuming you're using bash or another advanced shell. Posix doesn't define arrays, so #!/bin/sh -style scripts shouldn't have them. )


Also, please use [code][/code] tags around your code and data, to preserve formatting and to improve readability.

Last edited by David the H.; 12-07-2011 at 03:06 PM. Reason: added 2nd link and example
 
  


Reply

Tags
bash, redhat


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
[SOLVED] Checking variable against multiple values ksushants Linux - Newbie 6 03-02-2011 10:27 AM
grep multiple values in single pass through log file. 1ankit1 Programming 2 11-13-2010 05:45 PM
Multiple variable for loop (bash) Phier Programming 9 11-24-2009 09:54 AM
Multiple variable for loop (bash) Phier Linux - Newbie 2 11-23-2009 12:13 PM
Comparing Multiple Values in while loop rahulruns Programming 5 10-27-2009 06:59 AM


All times are GMT -5. The time now is 08:23 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration