LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 02-12-2007, 12:04 AM   #1
dkrysak
LQ Newbie
 
Registered: Sep 2004
Distribution: centos
Posts: 25

Rep: Reputation: 15
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!
 
Old 02-12-2007, 12:29 AM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,986
Blog Entries: 11

Rep: Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880
Have you looked m4 for these activities?


Cheers,
Tink
 
Old 02-12-2007, 11:33 AM   #3
dkrysak
LQ Newbie
 
Registered: Sep 2004
Distribution: centos
Posts: 25

Original Poster
Rep: Reputation: 15
Sorry - I do not understand your question... m4?
 
Old 02-12-2007, 12:27 PM   #4
SciYro
Senior Member
 
Registered: Oct 2003
Location: hopefully not here
Distribution: Gentoo
Posts: 2,038

Rep: Reputation: 51
Search google for m4, its a language that might be better suited to the task you want.
 
Old 02-12-2007, 05:21 PM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,986
Blog Entries: 11

Rep: Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880
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
 
Old 02-13-2007, 09:01 AM   #6
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 231Reputation: 231Reputation: 231
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".
 
Old 02-13-2007, 12:49 PM   #7
dkrysak
LQ Newbie
 
Registered: Sep 2004
Distribution: centos
Posts: 25

Original Poster
Rep: Reputation: 15
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!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading a conf file from a BASH script dinolinux Programming 5 08-03-2005 04:18 AM
Red Hat 9 server can't write PHP script, problem in httpd.conf file? x_menno_x Linux - Newbie 27 06-29-2004 04:44 PM
[FC2] How to write a modprobe.conf from a module.conf??? Coume Fedora 0 05-31-2004 03:40 AM
smb.conf file write permission jbraum Mandriva 3 01-09-2004 05:11 PM
Bash - Using variable from a .conf file steevy18 Programming 1 10-28-2003 11:58 AM


All times are GMT -5. The time now is 06:32 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration