BASH - How to access a variablized variable?
Greetings everone, I'm new here and need a bit of help.
I'm having an issue trying to pull the value out of a variable I have set. I have a loop that its variable is part of the name of a different variable. I can set it, but I cannot grab its value. My problem is, I don't know how to force BASH to treat the entire block of text as a single variable; it keeps breaking it into two. How can I make it treat it as one? Example - Code:
echo $STRING2_${STRING1}_REST_OF_STRING, it treats is as two variables. If I print it out, I get basically ${STRING1}_REST_OF_STRING with the $STRING2_ missing. I know it's doing it because it's splitting my variable into $STRING2_ as one variable (which is set to nothing) and ${STRING1}_REST_OF_STRING as a separate variable, which also is set to nothing. If I try - ${STRING2_${STRING1}_REST_OF_STRING} I get an error about bad substitution. I'm assuming since I can set the "big" variable, there is some built-in that I can use to force it to treat it as one variable and not two separate varaibles, but what is it? Google has left me empty handed so far. I've tried single quotes, double quotes, I've even got it to set to another variable properly, but then it treats that variable as a literal, and I cannot get it to print out its value. For example - Code:
NEW_STRING=`\$STRING_2${STRING1}_REST_OF_STRING` Help, please! Devon |
Hopefully this will help you:
Code:
sasha@reactor:~$ STRING1=MyName |
please, try to use arrays/associative arrays, now that newer bash supports them.
Code:
declare -a array |
If you already have or you can install bash 4.0, use associative arrays as ghostdog74 also suggested. It will really simplify things.
However if you still want to know how, perhaps you can play around with the ${!VAR} feature of bash. Code:
VAR=data |
Quote:
Code:
$ VAR=data |
hmmm.. my guess :-).. VAR also no? it's obvious why? .. to get data using VARPTR1.. we need to reparse it:
Code:
TEMP=${!VARPTR1} Code:
eval echo \"\${!${!VARPTR1}}\" and if i'm to use another variable pointer, i'll call it VARPTRPTR although in my projects i alwayse use *VAR like: Code:
ARRAY=() |
and VAR is "data" , right?
|
yup. ... i sort of don't get the point. they're just variables pointing to strings right? .. not really pointers.. only ${!PARAM} does the magic.
|
Quote:
|
Well it's enough if you're after only 1 level of reference. I don't really mean we should consider the method. I just gave the example for the single level.. It's up for the OP what he'll do .. either he still consider the re-reference thing or use eval.. but as I said before it's also better if he just use the hashes. You'll usually have to decide what other method to use when you're after at least more than 2 levels. I don't do that in my codes.
|
OK, the OP hasn't been back since asking this question.. Rather than perpetuate the discussion of 'this way is better, that way is better', I think we should wait for the OP's return and see if he/she has any input about the methods above. We don't even know what OS or shell the OP is using.
Frankly though, I don't see what was so difficult or horrid about my first reply, nor why using arrays would be (any/much) better. And, much of the above ideas imply not only a Bash shell, but assume that the OP has a new Bash shell, or is willing to get one; we know where assuming can lead us ;) @ Ghostdog, respectfully: your shell scripting help has many times proven very helpful to myself and others, and I appreciate that -- you're knowledgeable in this area, no question -- but on this subject of arrays, I'm getting a little bit of mixed messages from you. In this thread, you're suggesting use of arrays, yet in this other thread of mine you suggest I avoid (extensive use of) arrays and/or switch languages if I need a lot of array-like constructs :scratch: so why the difference? Was it the desire for POSIX-ness, or for portability, that your suggestion in that other thread was different? FWIW I'm working on a solution to that other thread today -- an update later I hope. Thanks for clarification :) -- on with the show, Sasha |
Quote:
whereas coming back to this thread, its a different problem. OP is trying to set a variable name using another variable, which essentially in programming terms, is equivalent to assigning values to arrays. consider this Code:
a=0 Code:
declare -a array of course, if the OP's shell doesn't support arrays/assoc arrays, then too bad. use the eval method. |
OK, gotcha -- thanks for that clarification. To our detriment, I had not been clear enough in that other thread though: I should have made it more clear that I am not "just starting" that project, but rather it's been ongoing for a long time, and just lately, I began considering making it more portable, but it already has made extensive use of arrays, so I'm looking at a "conversion" issue, rather than starting a fresh project in a portable way.
My apology for the lack of clarity there. Kind regards, Sasha |
Ah, i see. If you are still going to continue writing in shell, there are some books that you can take reference to. eg "Beginning Portable shell scripting" etc. Or search on the internet for articles about that. But personally, i think it will be worthwhile to port your project to Python, (or Perl). Besides making use of their good data structures, there are vast amount of libraries you can use for your project without reinventing the wheel. anyway, its up to you.
|
I can definitely confirm that arrays are one of the 3 basic data types (scalar, array, hash) in Perl & as he hinted, there's a huge library of pre-written modules avail at search.cpan.org.
Note that a lot of those are already in the std Perl install http://perldoc.perl.org/perlfaq3.htm...n-my-system%3F |
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 Code:
a=(a0 a1 a2 a3 a4) Code:
a["0,1"]=2 Code:
a0=(value1 value2 value3 value4) |
Further to the above, simulating arrays, even multi-dimentional ones, *can* be done like this:
Code:
a[0][1][2] Sasha |
Quote:
Devon |
All times are GMT -5. The time now is 04:30 AM. |