how to add Loop number to variable name?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to add Loop number to variable name?
# 1  
Old 05-21-2009
Question how to add Loop number to variable name?

Hi there,

I need to add a sequence of numbers to existing variables within the script to be able to call the variable automatically. Somehow it's just printing the loop number. Here's what I've got so far. Smilie

Code:
#!/bin/sh
FOLDER1=foo
FOLDER2=bar
FOLDER3=beer

for ((a=1; a <= 3 ; a++))
do
  n="$FOLDER$a"
  echo $n
done

I'm at lost on how to get that variable name together within the loop.

Any help would be greatly appreciated.

thanks guys.
# 2  
Old 05-21-2009
Quote:
Originally Posted by siul0_0
Hi there,

I need to add a sequence of numbers to existing variables within the script to be able to call the variable automatically. Somehow it's just printing the loop number. Here's what I've got so far. Smilie

Code:
#!/bin/sh
FOLDER1=foo
FOLDER2=bar
FOLDER3=beer

for ((a=1; a <= 3 ; a++))


That is not /bin/sh syntax. If you are using bash, why not use an array?
Quote:
Code:
do
  n="$FOLDER$a"

Code:
eval "n=\$FOLDER$a"

Quote:
Code:
  echo $n
done

This User Gave Thanks to cfajohnson For This Post:
# 3  
Old 05-21-2009
that worked like a charm! Thank you Smilie

PS: Well, I'm not using an array because I'm fairly new to shell scripts.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Add line number to for loop?

> cat test.sh for t in `cat out.txt` do echo create directory data as "'"$t"';" done > output of out.txt is as below /oracle/SID/data1/file2_1/ /oracle/SID/data1/file1_1/ /oracle/SID/data1/file5_5/ /oracle/SID/data1/file_9/ /oracle/SID/data1/file_2/ when i run my test.sh... (6 Replies)
Discussion started by: crazy_max
6 Replies

2. Shell Programming and Scripting

Number loop

Hello, I've been trying to create a list of numbers in bash that loops every 10s as follow. So the list will contain 1-5 on start, then 2-6 after 10s and so on. However when it reaches 30 it needs to start back at 1. 1 2 3 27 28 2 3 4 28 29 3... (2 Replies)
Discussion started by: shadyuk
2 Replies

3. Shell Programming and Scripting

[Solved] How to increment and add variable length numbers to a variable in a loop?

Hi All, I have a file which has hundred of records with fixed number of fields. In each record there is set of 8 characters which represent the duration of that activity. I want to sum up the duration present in all the records for a report. The problem is the duration changes per record so I... (5 Replies)
Discussion started by: danish0909
5 Replies

4. Shell Programming and Scripting

Array Variable being Assigned Values in Loop, But Gone when Loop Completes???

Hello All, Maybe I'm Missing something here but I have NOOO idea what the heck is going on with this....? I have a Variable that contains a PATTERN of what I'm considering "Illegal Characters". So what I'm doing is looping through a string containing some of these "Illegal Characters". Now... (5 Replies)
Discussion started by: mrm5102
5 Replies

5. UNIX for Dummies Questions & Answers

Loop and variable not exactly variable: what's wrong

Hello guys, This truly is a newbie question. I'm trying to make a loop to execute simultaneous commands indefinitely while using variable. Here is how my mess looks like (this is just an example): #!/bin/bash IP=`shuf -n 1 IP.txt` # I figured this would be easier to select random lines... (4 Replies)
Discussion started by: bobylapointe
4 Replies

6. Shell Programming and Scripting

printing variable with variable suffix through loop

I have a group of variables myLINEcnt1 - myLINEcnt10. I'm trying to printout the values using a for loop. I am at the head banging stage since i'm sure it has to be a basic syntax issue that i can't figure out. For myIPgrp in 1 2 3 4 5 6 7 8 9 10; do here i want to output the value of... (4 Replies)
Discussion started by: oly_r
4 Replies

7. Shell Programming and Scripting

[SHELL: /bin/sh] For loop using variable variable names

Simple enough problem I think, I just can't seem to get it right. The below doesn't work as intended, it's just a function defined in a much larger script: CheckValues() { for field in \ Group_ID \ Group_Title \ Rule_ID \ Rule_Severity \ ... (2 Replies)
Discussion started by: Vryali
2 Replies

8. Shell Programming and Scripting

how to add the number of row and count number of rows

Hi experts a have a very large file and I need to add two columns: the first one numbering the incidence of records and the another with the total count The input file: 21 2341 A 21 2341 A 21 2341 A 21 2341 C 21 2341 C 21 2341 C 21 2341 C 21 4567 A 21 4567 A 21 4567 C ... (6 Replies)
Discussion started by: juelillo
6 Replies

9. Shell Programming and Scripting

Number lines of file and assign variable to each number

I have a file with a list of config files numbered on the lefthand side 1-300. I need to have bash read each lines number and assign it to a variable so it can be chosen by the user called by the script later. Ex. 1 some data 2 something else 3 more stuff which number do you... (1 Reply)
Discussion started by: glev2005
1 Replies

10. Shell Programming and Scripting

loop number of records.

Initially i store some files into anothe file Y. Now i want read the contents of file Y one by one do some check on each file. i,e Open file Y (contains multiple files) First read a file , do some check on that individual file.If that file satisfies teh condition put it in another file. Now... (1 Reply)
Discussion started by: vasuarjula
1 Replies
Login or Register to Ask a Question