What are you trying to accomplish? Variable variables are the wrong answer. (I say that because I have not yet seen a case where they are the right answer.)
Assume you have a separate file, say
/etc/gateway.list:
Code:
EBS 192.168.0.1
OBIE 192.168.1.1
Now you can do e.g.
Code:
GW=""
while [ -z "$GW" ]; do
read -p 'Product? ' PROD
# Exit if empty product
[ -n "$PROD" ] || exit 0
# Locate product in gateway list
while read NAME IP COMMENT ; do
if [ "$NAME" = "$PROD" ]; then
GW="$IP"
break
fi
done < /etc/gateway.list
[ -n "$GW" ] || printf 'Product %s: No gateway found.\n' "$PROD"
done
which aborts the script cleanly if the user supplies an empty string for the product name.
I prefer to keep my syntax POSIX-compatible, but if you use Bash only, then you should probably use the
[[ ... ]] compound command for the conditional expressions; see the
Bash Reference Manual, Conditional Expressions chapter for details. The above snippet is written in a way which would allow it to work with both Bash and POSIX shells like dash. (I normally would use
awk to pick the IP address, but I decided to keep the above strictly within the shell, avoiding any external dependencies.)
If you don't want to have a separate file, replace the
done < /etc/gateway.list line with a here document, e.g.
Code:
done <<ENDOFLIST
EBS 192.168.0.1
OBIE 192.168.1.1
ENDOFLIST
Note that
ENDOFLIST must not be indented. (You can indent it with tabs if you use <<-ENDOFLIST, but since many editors convert tabs to spaces, and spaces will not work for the indentation, it is always simpler to put it in the first column even if it breaks indentation visually.)
A completely different approach in Bash is to have an array of IP addresses. Bash version 4 and later support associative arrays. To be compatible with older versions, you can just use an array with elements combining both name and address, i.e.
Code:
gateways=('EBS=192.168.0.1' 'OBIE=192.168.1.1')
in which case the loop to find the address matching PROD in the array would be
Code:
for entry in "${gateways[@]}" ; do
if [ "$PROD" = "${entry%%=*}" ]; then
GW="${entry#*=}"
break
fi
done
where
${entry%%=*} evaluates to the current value of
entry with everything after the first
= removed, and
${entry#*=} evaluates to the value of
entry with everything up to and including the first
= removed. (The value of
entry itself is not affected.) Note that this works fine with IPv6 addresses, too. It is the reason why I used = instead of : as the separator in the list.
So, what are you trying to accomplish?