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

  Unix/Linux 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 Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 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 Unix and Linux 01-09-2013
Yoda's Unix or Linux Image
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 24 February 2015, 2:15 PM EST
Location: Galactic Empire
Posts: 3,401
Thanks: 240
Thanked 1,215 Times in 1,141 Posts

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

Sponsored Links
    #3  
Old Unix and Linux 01-09-2013
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 4 March 2015, 3:53 PM EST
Location: Amsterdam
Posts: 9,961
Thanks: 321
Thanked 2,586 Times in 2,312 Posts
No need for patterns when we can use an exact parameter name:

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

    #4  
Old Unix and Linux 01-09-2013
Corona688 Corona688 is online now Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 4 March 2015, 5:19 PM EST
Location: Saskatchewan
Posts: 20,346
Thanks: 870
Thanked 3,564 Times in 3,329 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 Unix and Linux 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 Unix and Linux 01-09-2013
vgersh99's Unix or Linux Image
vgersh99 vgersh99 is offline Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 4 March 2015, 4:54 PM EST
Location: Foxborough, MA
Posts: 7,720
Thanks: 164
Thanked 611 Times in 572 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 Unix and Linux 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 Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Unix or Linux Image 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 06:42 PM.