File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Linux / UNIX and the fly likes dynamic variable assignment in bash Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Engineering » Linux / UNIX
Reply Bookmark "dynamic variable assignment in bash" Watch "dynamic variable assignment in bash" New topic
Author

dynamic variable assignment in bash

Thomas Murphy
Greenhorn

Joined: Dec 14, 2006
Posts: 23
Hi guys
I am stuck with what i would have thought to be a pretty easy part of a script.

basically i want to iterate a loop and assign a value to a dynamic variable:
my assignment and test are below:



the assignment is not working and i get an error:




so is there a way of assigning a value to a generated variable in bash?
i think my syntax is wrong judging by the error.
[ February 05, 2008: Message edited by: Thomas Murphy ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24057
    
  13

"eval" should work:

eval user$i=${line_array[i]}


[Jess in Action][AskingGoodQuestions]
Thomas Murphy
Greenhorn

Joined: Dec 14, 2006
Posts: 23
Thanks Ernest,
This has removed the error at least.

However i am referencing this variable now using: $user$i

This must be incorrect as it is still getting interpreted as 0 and not tim in the example above.
Thomas Murphy
Greenhorn

Joined: Dec 14, 2006
Posts: 23
As it happens the solution is the following:

eval echo \$user$i
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: dynamic variable assignment in bash
 
Similar Threads
Some JQuery not working Mozila Firefox 7.0
Help with jstl forEach spring 2.0 binding
How to pass dynamic value to resource bundle param like {0}
Please Help : Problem getting submitted value for dynamic input text field
Editing the bashrc file