Bash script dynamic file naming
Author Message
Posted : Mon, 05 January 2009 12:04:55
Subject : Bash script dynamic file naming
Hi All, This is my first post, im hoping someone can shed some light into this! I need to create a variable name on the fly then store information from a file to it e.g. -------------------------------------------------- #Get Details from ini file tmp=`cat "config.ini" | grep NumberOfLoadables=` #Store info arr_Load${x}_Type${j}=`echo ${tmp#*=}` -------------------------------------------------- But, when i run the script i get an error like this.. [b]arr_Load1_Type1=osver_db.xml: command not found[/b] I think the problem is with how "arr_Load${x}_Type${j}" is being create! I need to have the two indexes in the variable name and dynamically. Does anyone have any ideas?
thobbs
Posted : Mon, 05 January 2009 19:24:51
Subject : Re: Bash script dynamic file naming
I'm pretty sure you can't dynamically name a variable. Are you trying to create a pseudo 2D array of arr_Load_Type variables? Why do you not just create an actual 2D array? See here: [url]http://tldp.org/LDP/abs/html/arrays.html[/url], especially the section on "nested arrays". [Modified by: thobbs on January 05, 2009 01:29 PM]
blinky
Posted : Thu, 08 January 2009 18:26:27
Subject : Re: Bash script dynamic file naming
I put "NumberOfLoadables=abcde" into a config.ini Then tried tried: tmp=`grep "NumberOfLoadables=" config.ini` x=1 j=1 eval "arr_Load${x}_Type${j}=${tmp#*=}" echo $arr_Load1_Type1 and "echo $arr_Load1_Type1" prints abcde also eval echo "\$arr_Load${x}_Type${j}" prints abcde Not sure if that will help but I think you need to play with "eval" and use indirection to make it work (hope that makes sense).