How to make dynamic variable names for use in while loop?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to make dynamic variable names for use in while loop?
# 1  
Old 02-26-2013
How to make dynamic variable names for use in while loop?

Code:
i=0
while [ $i -lt $numberofproducts]
do

sizesfor0=`cat 16 | grep 'pickSize' -A 1 | grep '_sz' | cut -d'_' -f1`
sizesfor0=${sizesfor0//id=\"lll/:}
IFS=: array0=( $sizesfor0 )
echo ${array0[1]}
 i=$(( $i + 1 ))

done

So, right now I have two variables in the while statement above

sizesfor0 and array0

The above statement works, but it is hard-coded with the 0 suffix, how does one turn the above variables into sizesfor$i and array$i so that I can add 1 after each iteration to make the while loop functional?

I've tried doing exactly that, (changing to sizesfor$i and array$i) but there are compatibility issues.

Last edited by phpchick; 02-26-2013 at 05:38 PM..
# 2  
Old 02-26-2013
I'm leery enough about jamming everything into one variable, jamming multiple lines into multiple arrays -- plus your useless use of cat | awk | sed | cut | kitchen | sink makes me think you might be attacking this problem from the wrong angle.

This is shell. You don't have easy, complex datatypes, and array$i doesn't work and never will. The hacks that would allow it to work are invariably gargantuan security holes because the same code that would allow you to evaluate array$i would also evaluate array`rm -Rf ~/home`. You don't want to worry about arbitrary code execution every time you want to use an array.

If I absolutely had to store all that information I'd use one array. It's just as easy to split it later as it is this instant, so just put everything in the one array.

Code:
while [ $i -lt $numberofproducts ]
do
        ARR[$i]=whatever
done

But ideally, you could do your actual processing in that loop instead of storing everything and doing your processing later.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

FORTRAN: Loop over variable file names

Hi guys I'm a beginner in fortran. So excuse me for my naivety, let me briefly describe what I was trying to do. I have let's say 2 files named reac-1 and reac-2. After opening these files I've to do some calculations, close these files and open the same files again in a loop. So my faulty code... (6 Replies)
Discussion started by: saleheen
6 Replies

2. Shell Programming and Scripting

Matching the header of a .CSV file with dynamic field names

I have a .CSV file (frequency - weekly) whose header contains the year-week value in two of the columns which keeps changing every week. For an instance please see below. Column1,Column2,Column3,Column4,Column5,Column6,Column7,Column8,Column9,Column10,Column11,Column12,Column13,201420... (4 Replies)
Discussion started by: dhruuv369
4 Replies

3. 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

4. Web Development

MYSQL: Creating Dynamic Table Names 5.1

Hey everyone. Thanks for looking at this. I'm trying to create a table with the dynamic name of TableName + today's date. My variables are all happily created but the system chokes when I try to create the new table name example: Set @BFBW = CONCAT("BFBW", CURDATE()); Select @BFBW; ... (2 Replies)
Discussion started by: Astrocloud
2 Replies

5. Shell Programming and Scripting

how to make pattern search dynamic in awk

Hi, I have a data in a file like below - andy 22 abc 30000 wallstreet paul 30 xyz 40000 martstreet john 35 abc 50000 martstreet I want to search number of employees working in a particular company. Below query executes perfectly - awk '/abc/{ COUNT ++; }END { print "number of... (3 Replies)
Discussion started by: shell123
3 Replies

6. Shell Programming and Scripting

dynamic index for array in while loop

Hi, I'm just trying to use a dynamic index for some array elements that I'm accessing within a loop. Specifically, I want to access an array at variable position $counter and then also at location $counter + 1 and $counter + 2 (the second and third array positions after it) but I keep getting... (0 Replies)
Discussion started by: weak_code-fu
0 Replies

7. Shell Programming and Scripting

Problem with dynamic variables outside the loop

Hi All, Shell is ksh I've given portion of the script here to explain the problem. It will accept 2 input parameters . in_file1=$1 in_file2=$2 outbound_dir=/home/outbound for i in 1 2 do eval file$i=$outbound_dir/\$in_file$i eval echo "filename is \$file$i" ... (4 Replies)
Discussion started by: kk17
4 Replies

8. Shell Programming and Scripting

File Names in a Variable in a loop

Hi All , I am having confusion in a shell script. Please guide me. I need to get multiple files (number of files vary time to time, file names are separated by '|') using FTP get from the remote server. Actually, i call the FTP function in a loop. At the last step, i need to move all the get... (3 Replies)
Discussion started by: spkandy
3 Replies

9. HP-UX

dynamic names for .tar.gz files?

Hi All, I have a requirement to zip and compress files with dynamic names (which includes date and time component in their names) needs to be compressed with same name. for example I have a file T.30.SLS_ORD.SO.27.2_2_2.20080623.041415.dat which needs to archived as... (1 Reply)
Discussion started by: srinivas_paluku
1 Replies

10. Shell Programming and Scripting

how do I make dynamic parameter names? Or get the value of a parameter evaluated twi

Say I write something like the following: var1=1 var2=2 for int in 1 2 do echo "\$var$int" done I want the output to be: 1 2 Instead I get something like: $var1 $var2 (2 Replies)
Discussion started by: Awanka
2 Replies
Login or Register to Ask a Question