get var value when var name is part of another var
Hello,
I'm trying to write a script in bash. I have a for loop varlist="one two three" one_DIR=/directory1 two_DIR=/directory2 three_DIR=/directory3 for var1 in varlist do cd $"$var1_DIR" done var1 will contain part of a name to another variable that I want to get a value of. So I know the above code is wrong, but it illustrates what I'm trying to do. I want to change to a dir stored in the n_DIR variable and use the value in var1 to get the variable name. Is there a way to do this or should I abandon this plan and figure out another way? Thank you for any help. |
Code:
#!/bin/bash |
Indirect variable reference.
Code:
for var1 in $varlist Edit: oops... too late! :) Sorry for redundancy. |
Please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.
Indirect variables are not a generally recommended practice. It makes the code obscure. Modern shells usually have better ways to do most of the same jobs. How can I use variable variables (indirect variables, pointers, references) or associative arrays? http://mywiki.wooledge.org/BashFAQ/006 In addition, you should not store lists of things in a single variable. That's what arrays are for. In this case, I think all you should really need is a single regular array. Code:
dirlist=( /directory1 /directory2 /directory3 ) You could work with intermediate variables too, if you wanted, such as Code:
one=0 Code:
declare -A dirlist |
David - Thank you for the help, but I choose a string list for a number of reasons. Maybe I just don't know a trick to accomplish this using array's in bash. But mainly I chose not to use an array because I need to identify what values have been set and the kernal I'm using doesn't allow for the associative arrays (and I can't change this). I read through the links you posted and didn't see an alternative to what i need to accomplish. Searching a string seems to be faster then looping on an array to match a value. I also can't use a hard coded list of directories because that changes with each box being accessed. These have to be setup as parameters. I think the only answer for me is to indirectly access the variable value.
I do appreciate the info on the code tags. I was not aware of those but will be using them in the future. |
Quote:
|
Quote:
Quote:
But if you are defining the variable names/indexes entirely in the script, then this should really be a non-issue, since the problem only occurs if the variable names are defined from outside. If you are only using the names internally, there should be little need for any kind of indirection. Associative arrays don't suffer from this problem, naturally, as any string can be used as the index, and of course regular arrays simply use numbers to specify the entry. And I only used a loop for my examples because that's what you used. There's no need to loop through anything if you know the actual value you need to call. ( PS: Actually, varlist is a place where you should be using an array over a single variable. Again, scalar arrays are not designed for storing lists of things. Code:
varlist=( one two three ) Quote:
Code:
dirarray=( "$1" "$2" "$3" ) |
Quote:
Code:
dirlist[0]=/directory1 Code:
declare -A dirlist So that is not an option for me it seems. If it was then I wouldn't even be asking about how to use indirection. I'm not worried about what is stored in the variable for the indirection because it's all internal. Users do not have a way to change these values. |
I'm at a loss to see why it isn't working for you. bash 3.2 definitely has regular (numerically-indexed) arrays, although not associative ones. So the "0,1,2" indexed version should be working, at least, but the "one,two,three" one will not.
But what I'm really questioning is whether you need to have "word string" style variables at all, especially since you state that the whole thing is internal. I still suggest that a regular array would suit you just fine. You could even define the indexes it so that the index numbers start with 1 instead of 0. Code:
dirs=( [1]=/directory1 [2]=/directory2 [3]=/directory3 ) Code:
one=1 |
I appreciate the arguing. It makes me a better programmer and gives me new ways to think about things. And I think with your last idea about assigning numbers to strings gave me an idea of how I can change all of the affected code to use regular arrays. I don't think assigning numbers to strings will be the solution, but it put me on the right path. Who says arguing is a bad thing? :D Thank you for discussing with me.
|
All times are GMT -5. The time now is 08:14 PM. |