Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

creating a new variable out of another variable's value

30 views
Skip to first unread message

mike r

unread,
Oct 26, 2016, 4:26:02 PM10/26/16
to
Hi all, question about bash

lets say I have a var with a value

animal="tiger"

I now want to turn the value "tiger" into a variable itself, dynamically, like this

tiger="predator"
echo $tiger
# predator


Is there a way to go through an array of variables and use their values to create new vars, with each new.var.name=old.var.value

connor

unread,
Oct 26, 2016, 4:44:33 PM10/26/16
to
I use this method ... there are likely slicker/better methods to come.

[~]$ animal=tiger
[~]$ echo ${animal}
tiger
[~]$ eval ${animal}=predator
[~]$ echo ${tiger}
predator

Cheers,
Gary

Rakesh Sharma

unread,
Oct 27, 2016, 12:08:25 AM10/27/16
to
Bash has an operator just for your needs:

tiger="predator"
predator="foo"
echo "${!tiger}" ; # => outputs foo

Chris F.A. Johnson

unread,
Oct 27, 2016, 1:08:05 AM10/27/16
to
On 2016-10-26, mike r wrote:
> Hi all, question about bash
>
> lets say I have a var with a value
>
> animal="tiger"
>
> I now want to turn the value "tiger" into a variable itself, dynamically, like this
>
> tiger="predator"
> echo $tiger
> # predator

With bash, the easiest way is to use 'printf -v varname ...':

printf -v "$animal" %s predator

> Is there a way to go through an array of variables and use their
> values to create new vars, with each new.var.name=old.var.value

Loop through the array and use the above syntax.

--
Chris F.A. Johnson

mike r

unread,
Oct 27, 2016, 8:58:39 AM10/27/16
to
thanks for the tips, i used a "declare" and this worked.

I basically wanted to parse an external config file, and grab whatever parameters are in the config file, and then create bash vars with those parameters, dynamically. Im using an external Py script to parse the actual config file, since Py has built in ConfigParser module, which makes it easy to parse config values.



so for example, heres the conf

cat settings.conf
--------------------------------------------------------------------
[db]
name=xyz
user=joe

[app]
name=abc
user=rita
-------------------------------------------------------------------



cat process.sh // this is main bash script
--------------------------------------------------------------------
config="/etc/someplace/settings.conf"

declare -A params=( [db_name]= [db_user]= [app_name]= [app_user]= )

for param in "${!params[@]}"; do

# get param value from conf file, and create bash var
section=$(echo $param | awk -F"_" {'print $1'})
key=$(echo $param | awk -F"_" {'print $2'})
value=$(ConfigReader.py $config $section $key 2>&1)

params[$param]=$value

# create a bash var dynamically
declare "${section_${key}"=$value

# check if var is empty
if [ -z "${params[$param]}" ];
then
echo "Parameter ${param} is not set, check config file: "$config; echo "exiting.."; exit 1
fi
done

# test var output
echo $db_name
echo $db_user
echo $app_name
echo $app_user
-------------------------------------------------------------------

this is useful when theres a tons load of vars you need to initialize and set and you dont want to stuff them all into a single bash script.

Im working on a better version of this where the Py script reads in all params in the config file, and creates bash vars for everything that it finds, so you dont even need to create an associative array and declare the params manually

heres the Py config parser in case anyone needs it,

cat ConfigReader.py
---------------------------------------------------------------
#!/usr/bin/env python
# This reads a config file parameter and dumps out the value

import ConfigParser
import sys
from sys import argv
import os

conf_file=argv[1]
section=argv[2]
key=argv[3]

config = ConfigParser.ConfigParser()
config.read(conf_file)
value = config.get(section, key)
print value


Ian Zimmerman

unread,
Oct 28, 2016, 12:10:27 PM10/28/16
to
On 2016-10-27 05:58, mike r wrote:

> I basically wanted to parse an external config file, and grab whatever
> parameters are in the config file, and then create bash vars with
> those parameters, dynamically. Im using an external Py script to parse
> the actual config file, since Py has built in ConfigParser module,
> which makes it easy to parse config values.

> so for example, heres the conf
>
> cat settings.conf

> [db]
> name=xyz
> user=joe
>
> [app]
> name=abc
> user=rita

Is the cursed INI format imposed on you or can you choose what you use?

Pretty much _any_ other format will be easier to deal with.

Most recently I just use JSON, which I parse from shell scripts with the
invaluable jq tool.

--
Please *no* private Cc: on mailing lists and newsgroups
Personal signed mail: please _encrypt_ and sign
Don't clear-text sign: http://cr.yp.to/smtp/8bitmime.html

0 new messages