LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 01-05-2009, 07:28 AM   #1
teh_raab
LQ Newbie
 
Registered: Jan 2009
Posts: 4

Rep: Reputation: 0
Bash script dynamic file naming


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

arr_Load1_Type1=osver_db.xml: command not found


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?
 
Old 01-05-2009, 07:54 AM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
What does a "config.ini" file look like?
How does you script generate "x" and "j"?
How do you expect/need the values of the array members to look like? (e.g. arr_Load1_Type1="osver_db.xml")

Anyways, (guessing this information) you could try this:
Code:
arr_Load${x}_Type${j}="${tmp#*=}"
 
Old 01-05-2009, 07:59 AM   #3
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,458

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
You have to use eval to build the command line which does the assignment:
Code:
eval arr_Load${x}_Type${j}=$(echo ${tmp#*=})
in this way the shell makes the proper substitutions and then executes the resulting command. To retrieve the value of the variable, you have to use indirect reference in conjunction with eval:
Code:
eval echo \$arr_Load${x}_Type${j}
in this way the shell first substitutes ${x} and ${j} with their actual values, then substitutes the resulting variable name with its actual value.
 
Old 01-05-2009, 08:00 AM   #4
teh_raab
LQ Newbie
 
Registered: Jan 2009
Posts: 4

Original Poster
Rep: Reputation: 0
Hi Hko,

Thanks for your quick reply.

I am already using arr_Load${x}_Type${j}=`echo ${tmp#*=}` and the script seems to only complain about the arr_Load${x}_Type${j} part.

As for x and j, they are the index of the nested loop in which the command is in!

And the config.ini contains bits like this

NumberOfLoadables=2
NumberOfFiles=13
NumberOfXMLs=4

But a bit too complex to explain properly.
 
Old 01-05-2009, 08:09 AM   #5
teh_raab
LQ Newbie
 
Registered: Jan 2009
Posts: 4

Original Poster
Rep: Reputation: 0
Thanks Colucix, using the eval command seems to have worked.

Your saying to echo the content of the variable we have to use "eval echo \$arr_Load${x}_Type${j}"

could you explain why we cannot simply echo the result without the use of eval.

Thanks in advance.
 
Old 01-05-2009, 08:20 AM   #6
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,458

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
Just try it out. If you use echo without indirect reference (that is without escaping the $ sign) the shell interprets it as a simple string concatenation:
Code:
echo $arr_Load${x}_Type${j}
the command above substitutes $arr_Load, ${x} and ${j} with their actual values, being null the value of $arr_Load.

If you escape the first $ sign without using eval, the shell interprets it as a literal $
Code:
echo \$arr_Load${x}_Type${j}
and the resulting output will be something like "$arr_Load1_Type1".

Finally the eval statement forces the shell to do two substitutions: the first for ${x} and ${j} protecting the first escaped $ sign; the second for the resulting $arr_Load1_Type1 that is the dynamic variable name you're looking for.
 
Old 01-05-2009, 08:27 AM   #7
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,458

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
In addition, for a better understanding of what the shell is trying to do, you can run the script using bash -x. In this way you will get a detailed trace of all the commands executed by the shell. Very useful when doing some complex substitutions. For example, suppose you have a script called test.sh:
Code:
#!/bin/bash
x=1
j=1
echo Assign the dynamic variable
eval arr_Load${x}_Type${j}=$(echo ciao)
echo
echo Retrieve the value of the dynamic variable
eval echo \$arr_Load${x}_Type${j}
echo
echo Demonstrate the simple echo doing string concatenation
echo $arr_Load${x}_Type${j}
echo
echo Demonstrate the effect of the escaped \$ without eval
echo \$arr_Load${x}_Type${j}
echo
Running this script using the aforementioned method gives the following output
Code:
$ bash -x test.sh
+ x=1
+ j=1
+ echo Assign the dynamic variable
Assign the dynamic variable
++ echo ciao
+ eval arr_Load1_Type1=ciao
++ arr_Load1_Type1=ciao
+ echo

+ echo Retrieve the value of the dynamic variable
Retrieve the value of the dynamic variable
+ eval echo '$arr_Load1_Type1'
++ echo ciao
ciao
+ echo

+ echo Demonstrate the simple echo doing string concatenation
Demonstrate the simple echo doing string concatenation
+ echo 1_Type1
1_Type1
+ echo

+ echo Demonstrate the effect of the escaped '$' without eval
Demonstrate the effect of the escaped $ without eval
+ echo '$arr_Load1_Type1'
$arr_Load1_Type1
+ echo
 
Old 01-05-2009, 08:37 AM   #8
teh_raab
LQ Newbie
 
Registered: Jan 2009
Posts: 4

Original Poster
Rep: Reputation: 0
Thanks, that explaination really helped
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash script for listing FTP usage as the file name of a file created in each share jojothedogboy Programming 1 12-05-2008 03:35 PM
Adding dynamic command parameters in a Bash script haydenyoung Programming 3 08-28-2008 11:20 PM
best way to write a dynamic conf file in bash dkrysak Linux - General 6 02-13-2007 12:49 PM
Bash Shell Scripting Dynamic Variable naming question ZuG Programming 2 02-07-2007 02:39 PM
Bash file naming in makefile somnium Programming 1 10-18-2005 02:03 PM


All times are GMT -5. The time now is 07:59 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration