How to make dynamic variable names for use in while loop? | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

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

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-26-2013
phpchick phpchick is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 11 June 2014, 12:26 PM EDT
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
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 04:38 PM..
Sponsored Links
    #2  
Old 02-26-2013
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 28 July 2014, 6:27 PM EDT
Location: Saskatchewan
Posts: 18,988
Thanks: 750
Thanked 3,146 Times in 2,951 Posts
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.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
[SHELL: /bin/sh] For loop using variable variable names Vryali Shell Programming and Scripting 2 06-06-2012 10:47 AM
MYSQL: Creating Dynamic Table Names 5.1 Astrocloud Web Programming 2 11-29-2011 02:59 PM
File Names in a Variable in a loop spkandy Shell Programming and Scripting 3 06-11-2009 05:55 AM
dynamic names for .tar.gz files? srinivas_paluku HP-UX 1 06-23-2008 01:40 PM
how do I make dynamic parameter names? Or get the value of a parameter evaluated twi Awanka Shell Programming and Scripting 2 04-19-2007 09:37 PM



All times are GMT -4. The time now is 06:42 AM.