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
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!
> 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
..
>> 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