![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| foreach loop + 2 variables | JamesGoh | Shell Programming and Scripting | 3 | 06-04-2008 03:36 AM |
| using variables outside a while loop | Tornado | Shell Programming and Scripting | 2 | 02-09-2007 01:26 AM |
| adding variables for, for loop | jazz | High Level Programming | 3 | 03-13-2006 04:23 AM |
| Writing to variables within a loop | Sniper Pixie | UNIX for Dummies Questions & Answers | 0 | 03-02-2006 09:11 AM |
| for loop with multiple variables ? | fosterian | Shell Programming and Scripting | 3 | 03-01-2004 05:15 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
SH: two variables in for loop
Hi, say I have a simple sh script like this:
Code:
for i in a b c d
do
for j in 1 2 3 4
do
echo "$i $j"
done
done
Code:
a 1 a 2 a 3 a 4 b 1 b 2 b 3 b 4 c 1 c 2 c 3 c 4 d 1 d 2 d 3 d 4 Code:
a 1 b 2 c 3 d 4 |
|
||||
|
Thanks, I need to re-qualify my question:
what if its not a number and you can't do math on it? e.g. list1: mary eve delilah list2: joseph adam samson output: mary joseph adam eve samson delilah for i in `cat list1`; ... for j in `cat list2`; ... do ... print $i $j ... done any way around this? |
|
||||
|
Quote:
![]() Code:
paste -d ' ' list1 list2 Last edited by danmero; 07-30-2008 at 11:40 PM.. |
|
||||
|
It seems you can't have multiple "x in y" terms in a for loop, so the next best thing is probably to rewrite the loop as an index iterator:
Code:
is=(a b c d)
js=(1 2 3 4)
for ((i = 0; i < 4; i++))
do echo ${is[i]} ${js[i]}
done
![]() |
| Sponsored Links | ||
|
|