Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I've been struggling for a couple of days to understand how configuration files work in Linux but haven't really found any clear information regarding that.
My main question, as there is not a standard configuration file format in Linux, is how the format of a configuration file is understood by a Linux program? How the program reads its configuration file and how it understands its syntax (file format)? Also, a further question is how the program knows where its configuration file is being located?
For example inittab has its own "Identifier:RunLevel:Action:Command" format. How this is recognised by the "init" process?
Any help on the topic will be much appreciated.
Kind regards.
Click here to see the post LQ members have rated as the most helpful post in this thread.
You're kind of answering your own question. If there was a standard then you'd have a standard library which would interface with the config files and that would probably be how a program understands the config files. But as there is no such thing, then the files are just read however each developer / dev team decided to do it, using whatever code they feel like. So there is no answer to give you, it's just what the code does. Generally I'd say that you shouldn't really care, as there is no benefit in understanding this really, as far as I see.
Also, a further question is how the program knows where its configuration file is being located?
Generally, there are two ways a program knows where to find its configuration file.
1. The location is "hard-coded" into the program. All that means is the programmer explicitly coded the filesystem location and name for the configuration file inside the program itself.
2. The configuration file can be given as a command line argument. In this case, the program looks at its command line options, and if an option to specify an alternate configuration file is used, the program will use the filename on the command line.
Quote:
Originally Posted by pmil
My main question, as there is not a standard configuration file format in Linux, is how the format of a configuration file is understood by a Linux program? How the program reads its configuration file and how it understands its syntax (file format)?
Writing code to allow a program to understand data is called "parsing" and the programmer must explicitly tell the program how to do it. A program reads a file just like you read a document. The document is a "stream of letter/characters/symbols." When you see a sequence you recognize, you associate a meaning with that sequence and begin looking for the next sequence you recognize. That same process is used by a program. The program will look for recognized sequences of characters. If the sequence matches something the programmer told the program to look for, then the program executes another set of instructions based on the recognized sequence. Then the program goes back to look for another recognizable sequence until the end of the file is reached.
Thanks very much Dark_Helmet, your answer really helped me.
So, if I understood correctly, the program is fully responsible for reading (parsing) the configuration file correctly.
For instance, If I was about to write my own configuration file for an existing program (say "init" process), I should have modified the program accordingly so that it performs correctly according to the new configuration file. Is that right?
I was searching for source code of a Linux program that reads a configuration file and acts accordingly to see how this works in practice but I couldn't find any. Would you happen to know such a program?
One of my intentions is to write a simple program that uses a configuration file but it would help if there was a simple guide on that (mostly for the 'program' site).
Configuration files are not scripts, they don't necessarily "tell" a program to do anything at all. So either a program that "reads a configuration file and acts accordingly" is either, depending on your interpretation, ANY program that has a config file, or no program ever, as that would be a script, not a config file, and would be a different thing. In fact... an example of this would be bash. which can take a *script* as input and "act accordingly", i.e. run a sequence of bash commands. Same for other scripting languages, perl, python etc, but in order provide what could be examples, we've moved a long way from "configuration files". If you want an arbitrary example, I wrote a bizarrely popular program called acidrip, and in that I invent my own way to read and write attributes from a config file. Maybe have a look at my perl source code for it? I don't actually parse a file in that code, I maintain a set of variables in a data structure and simply print that data structure out to a file to update my config, and similarily just execute the config file on startup to make the variables in the file be available to my code. But really, there are a million different ways to achieve this, and in all of this, no one has actually mentioned what language you want to do this in... c? java? c++? perl? bash? ksh? python? perl? ruby? haskel? php? fortran? I expect that many of these languages will have third party (or maybe even built in) libraries for abstraction of config to a file - xml is a fairly common generic way of doing this, but they are always going to be voluntary.
depending on what you want your simple program to do, you can just open a file, read the contents and then do something because of what ever single word is it in. That would technically be a "configuration file" if you wish to see it as one, and could just be 3 or 4 lines of bash code.
Last edited by acid_kewpie; 02-05-2011 at 08:16 AM.
Configuration files are not scripts, they don't necessarily "tell" a program to do anything at all
I don't think I said that! I said that, as I understood, the program should decide how to parse a configuration file (if it is to parse any). The configuration file's layout/format does't instruct the program on how the latter should parse it.
Quote:
Originally Posted by acid_kewpie
If you want an arbitrary example, I wrote a bizarrely popular program called acidrip, and in that I invent my own way to read and write attributes from a config file. Maybe have a look at my perl source code for it?
Thanks, that would be much appreciated, although, I would prefer to see something written in C. Btw, I can't find the sources of your tool and to be honest, I would prefer a simpler example than that.
Quote:
Originally Posted by acid_kewpie
what language you want to do this in..
As I said, I would preferably use C language for doing it. What I basically want to know is how a program can successfully parse a configuration file.
Finally, a kind of general question: I was trying to find out the source code of the "init" program used on a System-V startup mechanism. I keep finding quotes like
Code:
“System V init examines the /etc/inittab file for an :initdefault: entry, which tells init
whether there is a default runlevel.”
but I can't find the actual code of the "init" program to examine how it parses the "inittab" file.
Sorry for the big number of questions and thanks again for all you help.
Whilst I totally understand what you're talking about, I don't understand WHY you're talking about it. Configuration files are not in themselves interesting, it's just a text file that you write. There are a thousand different reasons you might read or write data to or from a file, and you're at liberty to call any method you feel like using as your method of configuration files. You could absolutely store your data in the form of a PNG image if you so felt like... There are no specific standards, so you don't need to know about how to write config files, but how to read and write files in general.
Just to concur with acid_kewpie; a cfg file can be in any format you like, IF YOU are also writing the program that needs to 'parse' ie understand/use it.
If you want to create a copy of an existing cfg file and use it with the orig program, then you have to obey the current rules for that program/cfg file.
Typically a basic cfg file wuld be
var=value
& the prog needs to be able to read the file and assign the value specified to the equiv var inside the prog.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.