0

So take the following script as an example:

PROFILE1=profileName
SERVER1=serverName
PROFILE2=profile2Name
SERVER2=server2Name

VAR_$PROFILE1_$SERVER1=/home/$PROFILE1/$SERVER1/data
VAR_$PROFILE1_$SERVER2=/home/$PROFILE1/$SERVER2/data
VAR_$PROFILE2_$SERVER1=/home/$PROFILE2/$SERVER1/data

echo "$VAR_$PROFILE1_$SERVER1"

That is the basics of what I am attempting to accomplish. This may not be the best way to go about it, but for someone who is fairly new to bash scripting this was my first thought on how to do it.

I need to be able to only change one location in my script file and be able to add new "VAR_..." as needed for the system I am on.

Is this even possible? If not then if you understand what I am attempting to accomplish could you suggest an alternative?

2
  • So you want to, for example, generate the variable $VAR_profileName_serverName by replacing what $PROFILE1 and $SERVER1 contain? Commented Mar 19, 2014 at 15:19
  • @fedorqui correct, and be able to reference it later on Commented Mar 19, 2014 at 15:20

3 Answers 3

3

If you have bash version 4, use an associative array:

declare -A data
data=(
  [$PROFILE1,$SERVER1]=/home/$PROFILE1/$SERVER1/data
  [$PROFILE1,$SERVER2]=/home/$PROFILE1/$SERVER2/data
  [$PROFILE2,$SERVER1]=/home/$PROFILE2/$SERVER1/data
)
for key in "${!data[@]}"; do
    printf "%s\t%s\n" "$key" "${data[$key]}"
done
echo "${data[$PROFILE1,$SERVER2]}"
profileName,server2Name /home/profileName/server2Name/data
profileName,serverName  /home/profileName/serverName/data
profile2Name,serverName /home/profile2Name/serverName/data
/home/profileName/server2Name/data
Sign up to request clarification or add additional context in comments.

2 Comments

I like this approach much better than the way I was attempting to do it. Although the other approach mentioned by fedorqui works as well. Thanks!
I also have attempted this approach in my scenario and it worked out beautifully!
3

You can use eval declare for this (thanks Glenn Jackman for pointing in the good direction!):

$ declare VAR_${PROFILE1}_${SERVER1}="hello"
$ echo $VAR_profileName_serverName
hello

In your case:

declare VAR_${PROFILE1}_${SERVER1}=/home/$PROFILE1/$SERVER1/data
declare VAR_${PROFILE1}_${SERVER2}=/home/$PROFILE1/$SERVER2/data
declare VAR_${PROFILE2}_${SERVER1}=/home/$PROFILE2/$SERVER1/data

Note the use of VAR_${PROFILE1}_${SERVER1}. The curly brackets tell what's the exact name of the variable, because otherwise VAR_$PROFILE1_$SERVER1 would look for a variable named PROFILE1_, etc.

As echo VAR_${PROFILE1}_${SERVER1} returns VAR_profileName_serverName, then you can make use of the variables, using the following:

$ var="VAR_${PROFILE1}_${SERVER1}"
$ echo ${!var}
/home/profileName/serverName/data

Note that ${!var} is a variable indirection, that fetches the variable whose name is in the $var variable.

6 Comments

Ok, so to reference the VAR later on would it be possible to call "echo $VAR_$PROFILE1_$SERVER1"? The main reason is so that I do not have to copy and paste "serverName" all over the place, and keep it in one location.
@BrandonStout see my updated answer to see how to fetch the variables.
I would use declare not eval
Also, var="VAR_${PROFILE1}_${SERVER1}" is sufficient. You don't need to spawn an echo.
Ok thanks @fedorqui, I like the method described by glenn in his post better than my original method. But I was able to get this way to work as well. Thank you very much!
|
0

You mean this?

ABC="ahoj\$UNSETVAR"
echo $ABC
UNSETVAR=now_it_is_set
eval "DEV=$ABC"
echo $DEV

Output will be:

ahoj$UNSETVAR
ahojnow_it_is_set

Comments

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.