The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
.
google unix.com



UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to Read a config file and Assign to Variable redlotus72 UNIX for Dummies Questions & Answers 1 11-13-2006 12:14 PM
rename files using shell scripting gfhgfnhhn Shell Programming and Scripting 4 07-04-2006 04:37 PM
why shell scripting takes more time to read a file brkavi_in Shell Programming and Scripting 1 06-23-2006 08:20 AM
difference between AIX shell scripting and Unix shell scripting. haroonec Shell Programming and Scripting 2 04-12-2006 08:12 AM
new LAN, where are the config files? softarch UNIX for Dummies Questions & Answers 3 03-27-2001 02:36 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-26-2007
smallwonder smallwonder is offline
Registered User
  
 

Join Date: Feb 2007
Posts: 2
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 (permalink)  
Old 02-26-2007
sysgate's Avatar
sysgate sysgate is offline Forum Advisor  
Unix based
  
 

Join Date: Nov 2006
Location: Bulgaria
Posts: 1,318
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 (permalink)  
Old 02-26-2007
smallwonder smallwonder is offline
Registered User
  
 

Join Date: Feb 2007
Posts: 2
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 (permalink)  
Old 02-26-2007
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,944
Not sure whether this would be of help !

config_file

servername=abc
fielpath=/anotherdir

script

Code:
servername=`awk -F"=" '/^servername/ { print $2 }' config_file`
  #5 (permalink)  
Old 02-26-2007
gopidesaboyina gopidesaboyina is offline
Registered User
  
 

Join Date: Feb 2007
Posts: 29
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 (permalink)  
Old 03-02-2007
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
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"
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:43 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0