LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   best way to write a dynamic conf file in bash (https://www.linuxquestions.org/questions/linux-general-1/best-way-to-write-a-dynamic-conf-file-in-bash-527708/)

dkrysak 02-12-2007 12:04 AM

best way to write a dynamic conf file in bash
 
Hi there. I tend to write a lot of shell scripts to do installations and configurations of varios Linux software. Now because I want to dynamically write config files for systems (say for example postfix, etc), I am trying to figure out the most effective way to accomplish this. Does anyone have any suggestions? I would hate to have to write out echo statements. I would like to maybe have a "template" file in which the script would make a copy and substitute variables in the final output config file.

I was thinking of something like:

Code:

#!/bin/bash
# a var used in the template config file
declare -rx MYDB="postfix"
# a template config file
declare -rx TEMPATECFG="my.template.conf"
# The final config file with proper substitutions
declare -rx TMPFILE="my.conf"
# read in the template
while read curline; do
    echo "$curline" >> $TMPFILE
done < my.temnplate.conf

Then in my template file I would have something like:

Code:

grant SELECT ON $MYDB.*  to 'postfix'@'localhost' IDENTIFIED by 'somePass';
The actual code in the template file is really irrelevant to this question, and I was just providing an example.

Now obviously the above code does not work as the variable substitution does not happen.

So I guess I am just hoping to get a pointer on how others may have done this kind of thing.

Ultimately I just want to write dynamic config files.

Thanks!

Tinkster 02-12-2007 12:29 AM

Have you looked m4 for these activities?


Cheers,
Tink

dkrysak 02-12-2007 11:33 AM

Sorry - I do not understand your question... m4?

SciYro 02-12-2007 12:27 PM

Search google for m4, its a language that might be better suited to the task you want.

Tinkster 02-12-2007 05:21 PM

And to give some more input: m4 is a macro processor; it's been
designed for this kind of task where you can have "variables" in
files and get those replaced. It's somewhere between sed and perl :}



Cheers,
Tink

archtoad6 02-13-2007 09:01 AM

Does the technique in the example you gave work?

I can think of 2 other purely bash ways to populate .conf template w/ custom entries.

1st -- Use sed -i to do the replacements in the copy of the template. (Or awk.)

2nd -- Write the template(s) as series of functions that are called by the script after the template is sourced. I believe -- I didn't test this -- that the functions which are sourced would inherit the variables of the "parent" script.


If it's just that you hate, as I do, multiple echo statements, what about here docs?
--> man bash "Here Documents".

dkrysak 02-13-2007 12:49 PM

Quote:

Originally Posted by archtoad6
Does the technique in the example you gave work?


If it's just that you hate, as I do, multiple echo statements, what about here docs?
--> man bash "Here Documents".

No the technique I used does not work.... I really did not expect it to, I was however just trying to show what I was trying to accomplish.

I had thought of the here documents thing and tried it, but it was a misunderstanding on my part that caused me to abbanodon it, however I figured it out just now what I was doing wrong...

This works for my purposes:

Code:

#!/bin/bash
touch myconf.conf
cat > myconf.conf << POST_CONF
param1 = $1
param2 = $2
param3 = $3
POST_CONF
cat myconf.conf
rm myconf.conf

exit 0

What I had done wrong was a misunderstanding between redirection and the use of "<<" with a here doc. So when I was trying that in the first place - it was giving me issues!


All times are GMT -5. The time now is 04:49 AM.