What's the best way to read config files?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting What's the best way to read config files?
# 1  
Old 09-22-2011
What's the best way to read config files?

(copied directly from my other thread about something else)

At the moment, I just use...

cat config_file|grep var_name|cut -d'=' -f2

... which is fine for what I've been doing, but I'm not sure what to
do when there are a variable number of entries and I want it go through
them like a list.

Course, the config file itself could be a script but I prefer to keep
the configs seperate from the functions. Not really sure how to do
this. It's a problem sometimes, though I can usually hack up some kind
of work-around for my ignorance.
# 2  
Old 09-22-2011
You don't need to use cat ... read useless use of cat (dont worry everyone has been there)

How about providing a sample of your config_file. Might be really helpfull.
# 3  
Old 09-22-2011
I don't think there's a general best way. Personally, I prefer the way you seem to dislike, which is just sourcing a "script" which does nothing but define variables.

The big exception to this is when there's multiple targets, for example a connectivity check script that should treat different targets in different ways. The config would be something like
Code:
# This is a comment
# Host that's pingable
1.2.3.4|ping -c 3 -W 1 ${HOST}
# Host not pingable, but with a know port open
5.6.7.8|nc -w 3 -z ${HOST} 80

and read it like this:
Code:
grep -v '^#' ${config} | while IFS='|' read HOST COMMAND
do
    CMD=$( eval "echo $COMMAND" )
    $CMD
    if [ $? -eq 0 ]
.....
done

# 4  
Old 09-22-2011
Quote:
Originally Posted by ninjaaron
Course, the config file itself could be a script but I prefer to keep the configs seperate from the functions.
Don't put functions in your config files, then. :shrug: It's still the simplest and most direct way to do it, and the way its almost always done. You don't have to worry about comments or blank lines, the shell handles it.

Other alternatives usually involve eval which is, in the end, might as well just have sourced it since eval runs any shell statement, except that much harder for having to do all sorts of parsing yourself.

Last edited by Corona688; 09-22-2011 at 12:30 PM..
# 5  
Old 09-23-2011
Quote:
Originally Posted by maverick72
You don't need to use cat ... read useless use of cat (dont worry everyone has been there)
Interesting

So what do I use instead of cat for a line like this?

Code:
foo=`cat config_file|grep var_name|cut -d'=' -f2`

As to the question posting my config file, I don't have a specific one in mind. Something in another thread brought it to mind, and it's something I've been thinking about for a while.
------------------------------------------
As to the other comments:

Ok, I'll try doing it with scripts instead. The main config file I'm thinking provides variables for 17 different scripts (many of which both a) require the same variables and b) call eachother), so I'm not sure if it makes sense to have each of them loading all of the variables.,, I guess I could have any parent scripts load the proper variables into the sub-scripts... but some of them are called both independently and as a part of other operations. It's sort of complicated. since it's basically designed to shift my netbook between keyboard and tablet mode, and it manages all the settings for each mode, as well as things like rotating the screen and syncing the virtual keyboard with the screen orientation.

So far, I've just been "cat"ing all the required variables into each script as they are needed. I have no idea if this is the right way to do it. It's just what occured to me to do. That's sort of how all my scripting is. I just use the few commands I know to get the result I want. It usually works, but I'm sure there is all kinds of stupidity happening in the process(es?).
# 6  
Old 09-23-2011
Quote:
Originally Posted by ninjaaron
So what do I use instead of cat for a line like this?

Code:
foo=`cat config_file|grep var_name|cut -d'=' -f2`

Code:
foo=$( grep var_name config_file | cut -d'=' -f2 )

Quote:
Originally Posted by ninjaaron
Ok, I'll try doing it with scripts instead. The main config file I'm thinking provides variables for 17 different scripts (many of which both a) require the same variables and b) call eachother), so I'm not sure if it makes sense to have each of them loading all of the variables.,,
Do it like it's usually done with headers in C: conditional includes
Code:
if [ -z $CONF_FILE_LOADED ]
then
    var1=value1
    var2=value2
....
    CONF_FILE_LOADED=1
fi

So no matter how often you source it, the variables are set only once.
This User Gave Thanks to pludi For This Post:
# 7  
Old 09-23-2011
Another way to read a variable:
Config file:
Code:
efs=123

script snippet:
Code:
IFS='='  #set Input Field Separator
grep efs config_file | read var_name var_value
print "Variable: ${var_name}  value: $var_value"

Output:
Code:
Variable: efs  value: 123

This User Gave Thanks to gary_w For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read Data from Config file using Perl

Hi All, Can anyone please explain me how to read data from config file in Perl. Suppose i have a config file named cfile. The data in config file is name=parth lname=mittal user=2007 hostname=fluoride username=parthmittal password=XXXXXX account=unix url=www.unix.com ... (2 Replies)
Discussion started by: parthmittal2007
2 Replies

2. Shell Programming and Scripting

Read from config file and use it in perl program

Hi, I want to configure some values in config file like below work_dir /home/work csv_dir /home/csv sql_dir /home/sqls reportfirst yes and i want to store each value in variable to use it further in my my perl program ?? any thought on this(i am new to perl) ? ... (2 Replies)
Discussion started by: raghavendra.nsn
2 Replies

3. Shell Programming and Scripting

how to read the variable indivdually from config file

how to read the variable indivdually ( line by line ) in shell script eg : i have the config file where contain cat /home/user01/ilap/conf/input.conf node.txt node2.txt node3.txt i need to run script like /home/user01/ilap/exe/run.sh /home/user01/ilap/conf/input.conf ... (1 Reply)
Discussion started by: mail2sant
1 Replies

4. Shell Programming and Scripting

Shell script that will compare two config files and produce 2 outputs 1)actual config file 2)report

Hi I am new to shell scripting. There is a requirement to write a shell script to meet follwing needs.Prompt reply shall be highly appreciated. script that will compare two config files and produce 2 outputs - actual config file and a report indicating changes made. OS :Susi linux ver 10.3. ... (4 Replies)
Discussion started by: muraliinfy04
4 Replies

5. Programming

Read arguments from a config file in C

Hello everybody, I'm coding a program in C and i'm getting troubles with this. I need to read a config file and store the arguments into individual variables, let's say the config file looks like the following: #This is the configuration file... 192.168.0.1 A1:B1:C1:D1:E1:F1 192.168.0.2... (2 Replies)
Discussion started by: semash!
2 Replies

6. Shell Programming and Scripting

parsing config file to create new config files

Hi, I want to use a config file as the base file and parse over the values of country and city parameters in the config file and generate separate config files as explained below. I will be using the config file as mentioned below: (config.txt) country:a,b city:1,2 type:b1... (1 Reply)
Discussion started by: clazzic
1 Replies

7. Shell Programming and Scripting

Read from a config file.

Hello All, I have a config file which has the following data : export DBCHECKSUM001="/home/srvcdesk/DBCheckSum/DBCheckSum001.cfg" export DBCHECKSUM002="/home/srvcdesk/DBCheckSum/DBCheckSum002.cfg" export DBCHECKSUM003="/home/srvcdesk/DBCheckSum/DBCheckSum003.cfg" export... (1 Reply)
Discussion started by: Veenak15
1 Replies

8. Shell Programming and Scripting

how to read the variable from config file

how to read the variable from config file eg: a.cfg below config file config file which contain a=`hostname` b=250 a.sh is shell script echo "$a and $b" if i run "a.sh 1.cfg" it works but when i declare N number of variable it wont works.. can u please suggest for that ? (1 Reply)
Discussion started by: mail2sant
1 Replies

9. UNIX for Advanced & Expert Users

How to Read config (.cfg) files using shell scripting

Hello Friends I am new to this forum and this is my first post. I have to write a script in which i have to read data from a configuration ( .cfg) file. And also i have to execute/call some already written scripts from this script. Can you people plpease help me on this regards. Thanks... (5 Replies)
Discussion started by: smallwonder
5 Replies

10. UNIX for Dummies Questions & Answers

How to Read a config file and Assign to Variable

I have removeConfig file, it contains the dir paths for removing. I need to read line by line and assign to variable. any idea? (1 Reply)
Discussion started by: redlotus72
1 Replies
Login or Register to Ask a Question