Created
September 11, 2021 18:18
-
-
Save dlangille/ae407b1e7c3f52897114a580741ab3a9 to your computer and use it in GitHub Desktop.
Using variables for variables in a bourne shell script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Based on this Twitter thread: https://twitter.com/DLangille/status/1436388549985771520 | |
In a bourne shell, with VAR=ab and ab=123, can I get at the 123 value using ony VAR and not ab? | |
e.g. echo $VAR gives ab, but I want 123 | |
It's not ${!VAR} because I think that's bash. | |
$ echo ${!VAR} | |
/bin/sh: ${!V...}: Bad substitution |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
INGRESS_JAIL="ingress01" | |
WEB_JAIL="nginx01" | |
PG_JAIL="pg01" | |
DEFAULT_REPO_TREE="130amd64-default-primary" | |
eval ${INGRESS_JAIL}_REPO_TREE="${DEFAULT_REPO_TREE}" | |
eval ${WEB_JAIL}_REPO_TREE="${DEFAULT_REPO_TREE}" | |
eval ${PG_JAIL}_REPO_TREE="130amd64-default-pg13" | |
JAILS="$INGRESS_JAIL $WEB_JAIL $PG_JAIL" | |
for jail in ${JAILS} | |
do | |
eval repo_tree=\$${jail}_REPO_TREE | |
echo $jail will use $repo_tree | |
done |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ~/tmp/vars.sh | |
ingress01 will use 130amd64-default-primary | |
nginx01 will use 130amd64-default-primary | |
pg01 will use 130amd64-default-pg13 | |
Success. Thank you for coming to my TED talk. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment