[svlug] bash scripts - config files with comments?

Florin Andrei florin at andrei.myip.org
Thu Aug 16 14:09:34 PDT 2007

Often, when writing a shell (bash) script, I have to move portions of 
the script outside the main file and put them into a "config" file for 
that script. E.g., a script is doing something on a bunch of hosts, so I 
need to create a list with all the hosts. It's nice to put the list in a 
separate file, so as to not edit the main script too often.

But I'd like to be able to use a flexible format for those "config" 
files - something that allows to comment out lines (using the # 
character), ignore empty lines, be tolerant to the presence of the SPACE 
and TAB characters, etc. Something that's "syadmin-friendly".

The only way I could find is via an include file. E.g., to build a list 
of hosts, the main script is reading the "hostlist" config file and 
creates an array with all the hosts. The "config" file is:

hosts=( ${hosts[@]} "machine1" )
hosts=( ${hosts[@]} "machine2" )

See the problem? Instead of just adding a line with only "machine1", I 
have to use this nasty complex syntax.
Moreover, the "config" file is now part of the main execution loop, 
because it's an include file, which is bad.

So, is there a way to use "real", flexible config files from within a 
bash script?
What I need is to specify a list of things, one element per line, and be 
able to comment out lines, add empty lines (that get ignored), be able 
to mistakenly add TAB or SPACE to the lines and not get punished for it, 
The main script would parse the list into an array or something like that.

Of course this can be coded eventually, but it seems like a problem that 
someone, somewhere already solved, so why re-invent the wheel? (provided 
that it's invented already)

Florin Andrei


More information about the svlug mailing list