Reputation: 31
I have a shell script that consist of two files, one bash-file (main.sh) and one file holding all my config-variables(vars.config).
vars.config
domains=("something.com" "else.something.com")
something_com_key="key-to-something"
else_something_com_key="key-to-something else"
In my code i want to loop through the domains array and get the key for the domain.
#!/usr/bin/env sh
source ./vars.config
key="_key"
for i in ${domains[@]};
do
base="$(echo $i | tr . _)" # this swaps out . to _ to match the vars
let farmid=$base$key
echo $farmid
done
So when i run it i get an error message
./main.sh: line 13: let: key-to-something: syntax error: operand expected (error token is "key-to-something")
So it actually swaps it out, but i cant save it to a variable.
Upvotes: 0
Views: 225
Reputation: 2592
You can expand a variable to the value of its value using ${!var_name}
, for example in your code you can do:
key="_key"
for i in ${domains[@]};
do
base="$(echo $i | tr . _)" # this swaps out . to _ to match the vars
farmid=$base$key
farmvalue=${!farmid}
echo $farmvalue
done
Upvotes: 1