Pulling Parms from Config File | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Pulling Parms from Config File

UNIX for Dummies Questions & Answers


Tags
awk, config

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 01-09-2013
phunk phunk is offline
Registered User
 
Join Date: Nov 2012
Last Activity: 28 February 2014, 3:57 PM EST
Posts: 35
Thanks: 3
Thanked 1 Time in 1 Post
Pulling Parms from Config File

Hello all,

I'm working on a general script for something at work. I'm an up-and-comer backup for a Shell Scripter this company has had for 35 years lol. Anyway, I have a config file I'm trying to pull Variables from as the Config File is used for multiple scripts. Does the below make sense and is there any better way to do this?

Config File:

Code:
DayOfWeek Fri

Script:

Code:
function GetConfigParms
{
     DayOfWeek=`grep DayOfWeek app.main.cfg | awk '{print $2}'`
}

I appreciate it!
Sponsored Links
    #2  
Old 01-09-2013
Yoda's Avatar
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 26 November 2014, 4:40 PM EST
Location: Galactic Empire
Posts: 3,389
Thanks: 236
Thanked 1,210 Times in 1,136 Posts

Code:
DayOfWeek=$( awk '/DayOfWeek/{print $2}' app.main.cfg )

Sponsored Links
    #3  
Old 01-09-2013
Scrutinizer's Avatar
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 28 November 2014, 7:51 PM EST
Location: Amsterdam
Posts: 9,627
Thanks: 294
Thanked 2,459 Times in 2,205 Posts
No need for patterns when we can use an exact parameter name:

Code:
awk '$1==s{print $2}' s=DayOfWeek file

    #4  
Old 01-09-2013
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 28 November 2014, 4:08 PM EST
Location: Saskatchewan
Posts: 19,944
Thanks: 848
Thanked 3,422 Times in 3,207 Posts
Do you get to pick what your config file looks like? I'd have a config file like this:


Code:
DayOfWeek=Fri
SomethingElse=whatever

...which you could load directly just by doing . /path/to/configfile Note the space between the dot and the file. Dot is an operarator here, for 'source', which runs lines from that file in your own script.

If you're stuck with a config file like that one though, I might try this:

Code:
while read VAR VALUE
do
        if [ ! -z "$VAR" ]
        then
                read $VAR <<EOF
$VALUE
EOF
        fi
done < configfile

The trick is, read takes a variable name. So VAR=ABCD ; read $VAR reads into the ABCD variable.
Sponsored Links
    #5  
Old 01-09-2013
phunk phunk is offline
Registered User
 
Join Date: Nov 2012
Last Activity: 28 February 2014, 3:57 PM EST
Posts: 35
Thanks: 3
Thanked 1 Time in 1 Post
The ConfigFile is to my choosing. That's a similar format to how the guy I'm learning from had his, so I kept it. I prefer the

ParmName=Value

or

ParmName:Value

setup.

I appreciate your help guys! Note I only showed one Parm as an example, the file has many. I have it currently in multiple lines pulling it in. No loops, etc.
Sponsored Links
    #6  
Old 01-09-2013
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 28 November 2014, 1:09 PM EST
Location: Foxborough, MA
Posts: 7,672
Thanks: 153
Thanked 586 Times in 551 Posts
just follow the Corona688's suggestion with the paramName=paramValue format and 'sourcing' the config file - that's a better and an easier maintained paradigm.
Sponsored Links
    #7  
Old 01-10-2013
phunk phunk is offline
Registered User
 
Join Date: Nov 2012
Last Activity: 28 February 2014, 3:57 PM EST
Posts: 35
Thanks: 3
Thanked 1 Time in 1 Post
Oh ok. I talked with him and I guess he's making more work for himself. He sets an environment variables AND a config file separately, sourcing the env.vars and then using a function to pull in config parameters. I don't see why I can't just use one file and source it, as Corona said above. I feel like creating a function and grep'ping each damn variable isn't necessary.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Shell script that will compare two config files and produce 2 outputs 1)actual config file 2)report muraliinfy04 Shell Programming and Scripting 4 11-04-2010 08:30 AM
parsing config file to create new config files clazzic Shell Programming and Scripting 1 12-27-2009 03:06 PM
/etc/subsync parms definitions ? Browser_ice AIX 0 12-16-2008 04:38 PM
SUN OS - UDP parms nhatch UNIX for Advanced & Expert Users 1 09-05-2006 04:13 AM
Pulling out fields from a file Saz UNIX for Advanced & Expert Users 2 09-30-2001 04:31 PM



All times are GMT -4. The time now is 12:56 AM.