nested looping question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting nested looping question
# 1  
Old 09-25-2003
nested looping question

Hi,

I'm having some trouble with the syntax in constructing a simple nested 'for' loop.

My code is as follows:


#!/bin/bash

dir1="fred flume haystack"
for dir2 in ${dir1}
do
fred="1 2 3"
flume="a b c"
haystack="x y z"

for site in ${dir2}
do
echo ${box}/${site}
done
done

What I had expected was that in the first loop, ${dir2} would take the value of 'fred' which it seems to do OK. My thought was that in the nested loop, '${site}' would take the value of each value in dir2 (fred) ie. 1, 2 then 3.
Then the next ${dir2} would be read in (flume) and the process repeated so that flume's contents would be displayed (a, b then c) and so on...

Expected/desired output:

fred/1
fred/2
fred/3
flume/a
flume/b
flume/c
haystack/x
haystack/y
haystack/z

What i got:

fred/fred
flume/flume
haystack/haystack

Smilie

What am I doing wrong?
# 2  
Old 09-25-2003
Hi,

You are making many typos here. It might be worth looking at your script. Where did you define ${box} which you are echoing later on.

Plus. You have a

for dir2 in $dir
do
..
for site in $dir2
do

This makes no sense. The only thing what you do is fill $site with the value of $dir2. $dir2 only conatains one value a time.

Please re-read your script. I realy don't understand what you want to achief here.

Regs David
# 3  
Old 09-25-2003
sorry, 'box' should say 'dir2', and I've found a way round the problem anyway. Smilie

Thanks for your fast reply, only a few minutes after I posted. You guys are good! :P
# 4  
Old 09-25-2003
Just a side question pertaining to the original question .....

Is there a way in shell to use the string in a variable as the name of another variable like the original poster was trying to do ?? I know you can do it in perl like this:

$xxx = "yyy";
$yyy = "zzz";
$$xxx = "aaa"; <---- now $yyy contains "aaa"

How is this done in shell ?
# 5  
Old 09-26-2003
yeah, that's basically what I was trying to do as well, I even tried double dollar too ($$).

Can it be done in shell?
# 6  
Old 09-26-2003
Quote:
Is there a way in shell to use the string in a variable as the name of another variable
Yes, you can do this using the eval command. Use the search to find examples (there are 38 hits on eval).

syntax
VAR1=$(eval $VAR2)
# 7  
Old 09-26-2003
Quote:
Originally posted by google
Yes, you can do this using the eval command. Use the search to find examples (there are 38 hits on eval).
Maybe you should take another look at a few those hits. Smilie

The syntax that you posted will not work in a case like this. In this case, we need:

eval $xxx=\"aaa\"
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk nested looping?

I am trying to parse a text file and send its output to another file but I am having trouble conceptualizing how I am supposed to do this in awk. The text file has a organization like so: Name Date Status Location (city, state, zip fields) Where each of these is on a separate line in... (1 Reply)
Discussion started by: kellyanneghj
1 Replies

2. Shell Programming and Scripting

Nested looping statements

I cannot get the code below to work correctly. The IF statement works just fine, but not the looping. The inner loop tries to find files for a given vendor; if found, I need to sleep giving another process time to move the files. Once the given vendor's files are gone, then I want to move on to the... (1 Reply)
Discussion started by: dgreene
1 Replies

3. Shell Programming and Scripting

syntax question in regards to nested awk statements

Hello all, I am writing up an input file and I was hoping I could get some guidance as to how to best consolidate these 2 awk statements for 1 while loop. Here's my input file # cat databases.lst #NOTE: These entries are delimited by tabs "\t" #oracleSID name/pass # db11 ... (2 Replies)
Discussion started by: Keepcase
2 Replies

4. Homework & Coursework Questions

Help with looping question

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: I need to write a script that accepts 3 filenames as command line parameters and test if they are executable and... (1 Reply)
Discussion started by: tommynewb
1 Replies

5. UNIX for Dummies Questions & Answers

Bourne-sh (not bash) question about nested loops and sed

Here's the input: alpha, numeric or alphanumeric string ("line 1 string") numeric string ("line 2 string") numeric string ("line 3 string") numeric string ("line 4 string") ... where - each numeric string is in a pattern that can be matched with RE but - there can be any number of... (2 Replies)
Discussion started by: uiop44
2 Replies

6. Shell Programming and Scripting

Nested if question BASH

Just started learning bash ,and I am confused with sintaksis line 16: syntax error near unexpected token `else' thanks #!/bin/bash echo -n "Enter: " read num if(($(echo ${#num}) == 0 )) then echo No arguments passed.Try again elif rem=$(echo $num | tr -d ) ... (7 Replies)
Discussion started by: lio123
7 Replies

7. UNIX for Dummies Questions & Answers

Nested If question

if ]; then if ]; then rm -f ${LOGFILE}.old fi mv ${LOGFILE} ${LOGFILE}.old fi Havent done nested ifs in a while. I'm reading someones code If I'm reading this correctly. It checks for the logfile, and if it exists it checks for the old logfile and if that exists, it removes the... (8 Replies)
Discussion started by: NycUnxer
8 Replies

8. Shell Programming and Scripting

looping search question

This is my requirement-- I have a list file that contains a list of 50 words. eg word1 word2 word3 I want to search/grep for each of those words in a directory/and subdirectories and list the file in which that word exists. Ne ideas for script/command? I know grep -r <pattern>... (3 Replies)
Discussion started by: alfredo123
3 Replies

9. Shell Programming and Scripting

Looping question

Hi, I have series of data stored in a variable xyz: (between 0 and 100) example: 20 45 98 21..... I need to find if there is/are any occurance of data > 95 Not sure what kind of looping is required to check. Please help. thanks (2 Replies)
Discussion started by: hemangjani
2 Replies

10. UNIX for Dummies Questions & Answers

looping question

I am writing a simple script and want to keep the user in a fuction until they are ready to get out. For some (probably stupid) reason, it doesn't seem to be working. You guys see anything that I'm overlooking? crsd() {until do /home/wcs3611.crsdtmp.sh echo 'run another? \c' ... (1 Reply)
Discussion started by: hedrict
1 Replies
Login or Register to Ask a Question