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

Expand a configuration file (dynamic -> static)

0 views
Skip to first unread message

Daniel Oderbolz

unread,
Oct 1, 2008, 9:29:04 AM10/1/08
to
Dear Shellers,

I have a script that uses a hierarchy of configuration files.
At some point, new canfiguration files ae derived from a base.conf - this is
currently just a copy.

New assume base.conf consists of this lines:

MY_DIR=/some/dir/$(uname -n)
MY_STRING='$SOME_VAR'
MY_OTHER_STRING="$SOME_OTHER_VAR"

I am now looking for a way to expand unquoted variables and doublequoted
ones but leave single quoted ones alone.
So the derived config file would look like this:

MY_DIR=/some/dir/my_machine
MY_STRING='$SOME_VAR'
MY_OTHER_STRING=value_in_other_var

My issue are the single quotes - my current solution eats them, which is not
good (these variables get expanded much later).

Here is what I tried in bash:

while read line
do
echo $(eval "echo $(echo "$line")") >> derived.conf
done < base.conf

Thanks a lot,

Daniel

--
Daniel Oderbolz
Paul Scherrer Institut
CH-5232 Villigen

Laboratory of Atmospheric Chemistry,
Gasphase and Aerosol Chemistry Group
OFLA/007

Tel: +41 (0)56 310 2449
Fax: 4525
daniel.oderbolz [AT] psi.ch


edcrosbys

unread,
Oct 1, 2008, 11:25:30 AM10/1/08
to

Instead of doing this within the script (since I don't know how) I
would recommend seperating the variables you want to reuse and those
you don't (single quotes vs double) to seperate files, or seperate
part of the config file. That way you can move that portion over
seamlessly.

If you need to do this within the script, you may want to play with
delimiting the $ on the variables you'd like to keep as variables with
a backslash.

Hope this helps some!

Michael Schindler

unread,
Oct 1, 2008, 11:42:40 AM10/1/08
to
Daniel Oderbolz wrote:

> Dear Shellers,
>
> I have a script that uses a hierarchy of configuration files.
> At some point, new canfiguration files ae derived from a base.conf - this
> is currently just a copy.
>
> New assume base.conf consists of this lines:
>
> MY_DIR=/some/dir/$(uname -n)
> MY_STRING='$SOME_VAR'
> MY_OTHER_STRING="$SOME_OTHER_VAR"
>
> I am now looking for a way to expand unquoted variables and doublequoted
> ones but leave single quoted ones alone.
> So the derived config file would look like this:
>
> MY_DIR=/some/dir/my_machine
> MY_STRING='$SOME_VAR'
> MY_OTHER_STRING=value_in_other_var
>
> My issue are the single quotes - my current solution eats them, which is
> not good (these variables get expanded much later).
>
> Here is what I tried in bash:
>
> while read line
> do
> echo $(eval "echo $(echo "$line")") >> derived.conf
> done < base.conf
>
> Thanks a lot,
>
> Daniel
>


echo "$( unset ${!MY_*}; . ./base.conf ; set | grep ^MY_ )" > derived.conf


Daniel Oderbolz

unread,
Oct 2, 2008, 7:32:11 AM10/2/08
to

"Michael Schindler" <Michael....@materna.de> schrieb im Newsbeitrag
news:gc05pg$3n1$1...@pentheus.materna.de...

..


>> assume base.conf consists of this lines:
>>
>> MY_DIR=/some/dir/$(uname -n)
>> MY_STRING='$SOME_VAR'
>> MY_OTHER_STRING="$SOME_OTHER_VAR"
>>
>> I am now looking for a way to expand unquoted variables and doublequoted
>> ones but leave single quoted ones alone.
>> So the derived config file would look like this:
>>
>> MY_DIR=/some/dir/my_machine
>> MY_STRING='$SOME_VAR'
>> MY_OTHER_STRING=value_in_other_var
>>

...


> echo "$( unset ${!MY_*}; . ./base.conf ; set | grep ^MY_ )" > derived.conf
>
>

Dear Michael,

thanks a lot for this nice solution!
Can you elaborate what it does? You are basically turning off variable
expansion - but how do you get "normal" variables expanded?

edcrosbys: This approach does not work fer me because thue user will decide
what is static and what is dynamic.

Thanks a lot for your fast help, it is very appreciated!

Cheers,

Daniel


0 new messages