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 Code:
grant SELECT ON $MYDB.* to 'postfix'@'localhost' IDENTIFIED by 'somePass'; 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! |
Have you looked m4 for these activities?
Cheers, Tink |
Sorry - I do not understand your question... m4?
|
Search google for m4, its a language that might be better suited to the task you want.
|
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 |
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". |
Quote:
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 |
All times are GMT -5. The time now is 04:49 AM. |