LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-17-2009, 04:00 AM   #16
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235

Just want to add some more points as well. Using eval or ! is not bad in all forms. Sometimes there's no choice other than use them like when emulating multi-dimentional arrays.

We know that this is not possible:

Code:
a[0][1]=2
echo ${a[0][1]}
so sometimes we have to do things like this:

Code:
a=(a0 a1 a2 a3 a4)
a0[1]=2
eval echo \${${a[0]}}
we probably can make a cleaner version to that with associative arrays like

Code:
a["0,1"]=2
echo ${a["0,1"]}
i=0 j=1
echo ${a["$i,$j"]}   # correct me if the syntax of the index is incorrect.
but we know in associative arrays, we cannot associate many values as once as in indexed arrays and order of indices in associative arrays is never a guarantee. I'm not sure if it's sorted by default in bash.

Code:
a0=(value1 value2 value3 value4)
bvalues=<input from somewhere with values: value5 value6 value7 value8 in order>
b0=(${bvalues})
for A in ${a0[@]}; do ...
There are also many other situations where only indexed arrays can be applicable.
 
Old 12-17-2009, 11:01 AM   #17
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
Further to the above, simulating arrays, even multi-dimentional ones, *can* be done like this:
Code:
a[0][1][2]

would become:

a012 or a_0_1_2 (or however you want to write it). To put a value in there:

shell$ a012="element 0,1,2"

To do it using some arbitrary index:

shell$ x=2 # index
shell$ eval a01${x}="'element 0,1,2'"

to get an indexed value out:

shell$ eval echo \$a01${x}

further example with two indexes:

shell$ y=5;x=2
shell$ eval a0${y}${x}="'element 0,5,2'" # assign a value
shell$ eval echo \$a0${y}${x} # read the value

further example with 3 indexes:

shell$ z=6
shell$ eval a${z}${y}${x}="'element 6,5,2'" # assign a value
shell$ eval echo \$a${z}${y}${x} # read the value
NOTE: Be careful to use enough "quotes" when assigning multi-part strings to the 'arrays' this way. Unquoted multi-part strings/values will give an error when assigned like this.

Sasha

Last edited by GrapefruiTgirl; 12-17-2009 at 11:04 AM.
 
Old 12-18-2009, 01:59 PM   #18
DevonB
LQ Newbie
 
Registered: Dec 2009
Posts: 27

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by GrapefruiTgirl View Post
Hopefully this will help you:

Code:
sasha@reactor:~$ STRING1=MyName
sasha@reactor:~$ SOME_VALUE=Devon
sasha@reactor:~$ newvar=$(eval echo \${STRING2_$STRING1_REST_OF_STRING=$SOME_VALUE})
sasha@reactor:~$ echo $newvar
Devon
sasha@reactor:~$
sasha@reactor:~$ eval echo \${STRING2_$STRING1_REST_OF_STRING}             
Devon
sasha@reactor:~$
Sasha
...and it worked perfectly, thank you so much.

Devon
 
  


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: HISTSIZE: readonly variable -bash: HISTFILESIZE: readonly variable deathsfriend99 Linux - Newbie 4 12-08-2009 01:51 PM
How to get variable from text file into Bash variable mcdef Linux - Software 2 06-10-2009 02:15 PM
Problem with bash script - variable name within variable name steven.c.banks Linux - Newbie 3 03-10-2009 04:08 AM
BASH: instad of echo-ing, I just want to assing to a bash variable... how?? rylan76 Linux - Newbie 9 11-28-2008 09:46 AM
bash scripting need help getting a variable of a variable dovo Programming 6 10-29-2008 02:30 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration