This is a problem where in you a have a config file and you have wrapper script and a child script where in your wrapper and child script are generic enough to process certain task depending on the parameter you pass to create your dynamic variable and source your static value from a config file, this way you wouldn't need to touch your scripts but only the config.
I found a lot of ways through googling easiest would be the bash solution but fortunately i am using ksh so another one was the use of eval, however most of the solutions i found using eval on the net didn't work for me, either i didn't know how to use or apply it, so i tried a few combinations and below is what worked for me :
If your doing it in bash shell its quite easy :
so you see the trick if you are in bash is just "!".
but if we are doing in ksh, then its a different story.
you will need eval, here is how i accomplish the task, in the example below i have 3 files, config, parent and child script:
in the parent script it accepst parameters dev, test and prod to access each value for each specific variable and pass to the child script.
executing the scripts will result into the following :
There you go, so the trick in ksh is done by the eval function.
it evaluates the result of a given command. I haven't looked it up yet, so check out the the man page for eval and you tell me or leave a comment. :)
I found a lot of ways through googling easiest would be the bash solution but fortunately i am using ksh so another one was the use of eval, however most of the solutions i found using eval on the net didn't work for me, either i didn't know how to use or apply it, so i tried a few combinations and below is what worked for me :
If your doing it in bash shell its quite easy :
dev_config_var=abc;
dynamic_value=dev;
dynamic_var=${dynamic_value}_config_var;
echo ${!dynamic_var}
so you see the trick if you are in bash is just "!".
but if we are doing in ksh, then its a different story.
you will need eval, here is how i accomplish the task, in the example below i have 3 files, config, parent and child script:
bash-4.1$ cat test.configexport dev_config_var=abcexport test_config_var=defexport prod_config_var=xyzbash-4.1$ cat parent_script.sh#!/bin/ksh. `pwd`/test.configparam1=$1dynamic_var=`eval echo '$'${param1}_config_var`export dynamic_var./child_script.shexit 0
so you see in the setup of the config i have 3 variables, dev, test, prod config_var.bash-4.1$ cat child_script.sh#!/bin/kshecho "$dynamic_var"bash-4.1$
in the parent script it accepst parameters dev, test and prod to access each value for each specific variable and pass to the child script.
executing the scripts will result into the following :
There you go, so the trick in ksh is done by the eval function.
it evaluates the result of a given command. I haven't looked it up yet, so check out the the man page for eval and you tell me or leave a comment. :)
Comments
Post a Comment