LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 04-07-2008, 11:18 AM   #1
piercey
LQ Newbie
 
Registered: Sep 2007
Distribution: Gentoo, RHEL 5.2, CentOS 5
Posts: 23

Rep: Reputation: 15
Bash Scripting - Configuration files


Hi guys,

I want to write a portable script using BASH builtins that can be used to set network and host information on a number of (old) Red Hat machines.

Each machine has a number of interfaces, some of which are used on some machines and some on others. I want to try create a configuration profile for each machine so my script can apply relevant information for each machine.

This is where I need some suggestions on the best way to do this using Bash. Normally, I'd write an XML file like so:

Code:
<server host="SERVER1">
 <interface name="eth0" enabled=true>
  <IP>192.168.1.100</IP>
  <NETMASK>255.255.255.0</NETMASK>
  <GATEWAY>192.168.1.1</GATEWAY>
 </interface>
 <interface name="eth1" enabled=false>
 </interface>
 <interface name="sw0" enabled=true>
   ..etc...
 </interface>
</server>
I'd then loop through each interface and act accordingly. Is it possible to come up with a similar implementation but using Bash tools only? I don't want to have to install extra packages on these machines so it can use this script.

Thanks a lot,
 
Old 04-07-2008, 11:21 AM   #2
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
I'd use sed with regular expressions to parse. You'd need some sort of loop, and maybe a function to go with each specific tag. How does this information interface with the script? Does the script have to select an "interface" group, then use all of the components of it, or does the script take the entire file into account?
ta0kira
 
Old 04-07-2008, 11:56 AM   #3
piercey
LQ Newbie
 
Registered: Sep 2007
Distribution: Gentoo, RHEL 5.2, CentOS 5
Posts: 23

Original Poster
Rep: Reputation: 15
Hi ta0kira,

I think for simplicities sake the machines will each have a profile, like so:

./profiles/<server>/network.conf

So all the network information would be stored in that one file (using some process to read it) for that one particular server. This way I'd be able to use seperate configs for other things on the server also - networking is just one thing I want to cover . The advantage to this would be the ability to drop a profile onto a machine and use it with my script.

I know I could also just copy and replace the /etc/sysconfig directory (or its individual cfgs) for each machine, but I'd prefer to do it this way so it could be more portable. I guess using sed is the most likely choice for me, but I'm still open to suggestions

Thanks again,
 
Old 04-07-2008, 01:04 PM   #4
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
The hardest part about parsing XML-style files is retaining the context of embedded tags. That will be fairly difficult with a script unless you have a finite, low number of allowed levels of embedding (2 or 3 at most) and a very limited number of options for parent/child combinations. Using your example as my example, you would probably want to limit possible child sections of server to interface and maybe 1 or 2 others, limit the possible children of interface to netmask and gateway, etc. and never allow arbitrary embedding, such as server being a child of netmask. You'll need to be very restrictive in this manner to make it feasible with a shell script.

This type of thing isn't much easier in a C++ program. The hardest part is always going to be aligning the program or script with the context of each point of data, and that's especially difficult in a script where you have a very limited ability to create structure and essentially no nonlinear interaction with the user or programs.
ta0kira

Last edited by ta0kira; 04-07-2008 at 01:33 PM.
 
Old 04-07-2008, 01:38 PM   #5
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
I wouldn't want to parse XML without a proper XML parsing library. Doing it with your standard shell text tools (sed, grep, awk et al.) is a pain in the rear and error-prone. If your program is complicated enough to need a really structured XML file format, it seems likely to me that shell scripting isn't going to be an ideal way to implement it. Given that sort o task, I'd be looking at doing it in Perl, using one of the XML parsing modules to do the heavy lifting for me.

If all you have is a table of values: interface, enabled_status, ip, netmask and gateway, then use a delimited file and then you can iterate over it line by line using IFS to split the fields. You can do this with a small, clean simple shell script which will be easy to maintain. Delimited files can be imported into spreadsheets and and databases, and is a piece of cake to handle in your own programs. XML is wonderful if you have a very complicated structure, but it is much more complicated. I like to keep it simple.
</TwoPennies>
 
Old 04-07-2008, 01:58 PM   #6
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
There are a lot of easy ways to parse XML. The problem is representing it in a way that conforms to its use. For example, you might need it represented as a hierarchy identical to the file's structure, or you might want to digest it and tabulate it as you go (e.g. a compiler,) or you might want to merely establish a context for a specific element. No single library is suitable for all of this, but most are more suitable than a shell script.
ta0kira
 
Old 04-07-2008, 11:28 PM   #7
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 719

Rep: Reputation: 72
Hi.
Quote:
Originally Posted by matthewg42 View Post
... XML is wonderful if you have a very complicated structure, but it is much more complicated. I like to keep it simple.
</TwoPennies>
Me too; if writing in perl, I usually follow Conway's advice:
Quote:
Don't use XML as your configuration file format. It may be
human-readable, but it's almost never human-understandable, and
the ratio of mark-up to content is vastly too high.

All three of these alternatives [ Config::{General,Std,Tiny} ]
allow you to read configuration files into an internal data
structure, update that data structure, and then write it back out
in the appropriate configuration file syntax.

Perl Best Practices, D Conway, O'Reilly, 2005; page 445 ff.
I haven't seen similar tools for the shell, however ... cheers, makyo
 
Old 04-08-2008, 05:13 AM   #8
piercey
LQ Newbie
 
Registered: Sep 2007
Distribution: Gentoo, RHEL 5.2, CentOS 5
Posts: 23

Original Poster
Rep: Reputation: 15
Cheers for the input guys, I'll have a look into the Perl and Config::
 
Old 10-17-2008, 11:41 AM   #9
biiiep
Member
 
Registered: Aug 2003
Posts: 39

Rep: Reputation: 15
perhaps xmlcatalog can help you. It's a command line tool you can invoke from your bash script.
 
  


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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to burn/write files in a cd-r/rw using bash scripting jaepi Linux - Newbie 7 05-06-2007 08:50 PM
Bash Scripting: Editing external files trek413 Linux - Software 1 11-02-2006 05:11 PM
moving files that have spaces in variables -bash scripting bhar0761 Programming 10 09-22-2005 08:30 AM
bash scripting - editing files brian0918 Linux - Newbie 2 07-01-2003 03:27 PM
bash scripting - editing files brian0918 Programming 1 06-30-2003 07:16 PM


All times are GMT -5. The time now is 06:41 AM.

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