Unix/Linux Go Back    

Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

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

Shell Programming and Scripting

Thread Tools Search this Thread Display Modes
Old Unix and Linux 02-26-2013   -   Original Discussion by phpchick
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?

while [ $i -lt $numberofproducts]

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


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..
Sponsored Links
Old Unix and Linux 02-26-2013   -   Original Discussion by phpchick
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
Join Date: Aug 2005
Last Activity: 21 November 2017, 3:22 PM EST
Location: Saskatchewan
Posts: 22,518
Thanks: 1,154
Thanked 4,273 Times in 3,946 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.

while [ $i -lt $numberofproducts ]

But ideally, you could do your actual processing in that loop instead of storing everything and doing your processing later.
Sponsored Links

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 11:47 AM
MYSQL: Creating Dynamic Table Names 5.1 Astrocloud Web Programming 2 11-29-2011 03:59 PM
File Names in a Variable in a loop spkandy Shell Programming and Scripting 3 06-11-2009 06:55 AM
dynamic names for .tar.gz files? srinivas_paluku HP-UX 1 06-23-2008 02: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 10:37 PM

All times are GMT -4. The time now is 08:50 AM.