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


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users How to Read config (.cfg) files using shell scripting
# 1  
Old 02-26-2007
Question 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 in Advance.

Manish
# 2  
Old 02-26-2007
It really depends what are you trying to achive, which language will be used, what is the underlying OS, etc....
example :
I have one file called "config.cfg" and I read it like this :
Code:
if __name__ == "__main__":
# some stuff
  try:
    configFile = open('config.cfg')
    config.readfp(configFile)
  except IOError:
    print "Config.cfg file not found."
    exit()

This example is Python, reading from file on most languages is similar.
# 3  
Old 02-26-2007
Hello Friend

Thanks for reply

I am using Unix/Solaris plateform

config file format is like this
########################
config.cfg
servername =
filepath =

student name1
#student attributes
age =
class=

student name2
age =
class =

####################

i have to use unix shell scripting.
first i have to read that server and filepath..

after that i have to access these students and their corresponding attributes.

so now can you please help me further.

Thanks in advance.

Manish
# 4  
Old 02-26-2007
Not sure whether this would be of help ! Smilie

config_file

servername=abc
fielpath=/anotherdir

script

Code:
servername=`awk -F"=" '/^servername/ { print $2 }' config_file`

# 5  
Old 02-26-2007
if you can get the config file like this..you may use below script.

config file format is like this
########################
config.cfg
servername =
filepath =


#student attributes
student name1 age =
student name1 class=

#student name2
student name2 age =
student name2 class =

###############################


while read line < configfile
do
`echo $line | awk -F"=" '{print $1}'`=`echo $line | awk -F"=" '{print $2}'`

# This would assign servername = < The value you specified > & so for all..
done

Now use those varibales in your script after the loop...
# 6  
Old 03-02-2007
Quote:
Originally Posted by smallwonder
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.

How you read the data depends on how the .cfg file is written. I use .cfg files written as shell scripts which I just source, e.g.:

Code:
. ${0##*/}.cfg

If the file is not a valid shell script, you will have to read it line by line:

Code:
while IFS= read -r line
do
  : do whatever is necessary to interpret the line here
done < "$configfilename"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to read multiple files at same time through UNIX scripting?

How to read multiple files at simultaneously? (1 Reply)
Discussion started by: Priyanka_M
1 Replies

2. UNIX for Dummies Questions & Answers

Shell script to read lines in a text file and filter user data Shell Programming and Scripting

sxsaaas (3 Replies)
Discussion started by: VikrantD
3 Replies

3. Shell Programming and Scripting

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... (7 Replies)
Discussion started by: ninjaaron
7 Replies

4. Shell Programming and Scripting

Request for file read option in Unix shell scripting

Hi Friends, I would like to read all the record from one txt file to other file txt For example I have two txt file a.txt and b.txt. I need to read a.txt record by record and I need add the system date @ end of each record before moving it to b.txt. Could you please share the coding for... (4 Replies)
Discussion started by: vinoth124
4 Replies

5. 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

6. Shell Programming and Scripting

Help with dynamic configure cfg files

hi; i have one configuration file(configuration.cfg),where contents are below.. filename = charge.cfg sectionname = networkid = 1 retrytimes = 2 ------------- -------------- sectionname = networkid = 1 retrytimes = 2 filename = xyz.cfg ------------------ ----------------- There is... (7 Replies)
Discussion started by: suryanarayan
7 Replies

7. Shell Programming and Scripting

how read the flat files from shell scripting

We are using sybase data base and we have the data base name called MasterDB. The data baase MasterDB contains 100's of tables like sample1, sample2, sample3...etc...sample100 To take the count of every table we have to execute the following commands use MasterDB //DB name go //execute... (1 Reply)
Discussion started by: shijoe
1 Replies

8. UNIX for Advanced & Expert Users

how read the flat files from shell scripting

We are using sybase data base and we have the data base name called MasterDB. The data baase MasterDB contains 100's of tables like sample1, sample2, sample3...etc...sample100 To take the count of every table we have to execute the following commands use MasterDB //DB name go //execute... (1 Reply)
Discussion started by: shijoe
1 Replies

9. UNIX for Advanced & Expert Users

how to config ks.cfg for install another progra.

i create ks.cfg for automatic install for centos 5.2 it's work perfectky for cutom install linux package. and i want to install another program with shell scripts (assume test.tar include in DVD linux custom package)** single dvd include linux os and test.tar sample #!/bin/bash... (1 Reply)
Discussion started by: slackman
1 Replies

10. Shell Programming and Scripting

why shell scripting takes more time to read a file

i have done a coding in shell scripting which reads a file line by line and does something....it takes more than 30 seconds to execute for a single file. when i do the same with perl scripting it takes less than a second. is that shell scripting is not efficient while working with large number of... (1 Reply)
Discussion started by: brkavi_in
1 Replies
Login or Register to Ask a Question