2

I'm trying to make a script so that I can easily add host types to it without changing the code itself. Each type of host has a specific group of id's and templates associated with it. Here is the cod that I have derived:

CLASS="memcache"
memcache_template=( 42 45 )
CLASS_template=${CLASS}_template
template=$( eval echo $`echo $CLASS_template` )

for i in  ${template[@]}; do
  echo $i
done

The output that I get is just "42". I need it to output both 42 and 45.

1 Answer 1

3

Here's a way to do it:

CLASS="memcache"
memcache_template=(42 45)

CLASS_template=${CLASS}_template[@]

for i in ${!CLASS_template}; do
  echo $i
done

See the discussion of variable indirection in info "(bash)Shell Parameter Expansion". Note that you cannot use ${!CLASS_template[@]} because that has a special meaning. The array subscripting must be done before indirection.

Sign up to request clarification or add additional context in comments.

2 Comments

Neat! Also, you could put memcache_template's contents into a new array (more similar to the code in the question) with template=("${!CLASS_template}")
Actually, I found that I can skip a whole step. I simply defined template=( "${CLASS}_template[@]" ).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.