|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
||||
|
||||
|
Code:
DayOfWeek=$( awk '/DayOfWeek/{print $2}' app.main.cfg ) |
| Sponsored Links | ||
|
|
#3
|
||||
|
||||
|
No need for patterns when we can use an exact parameter name: Code:
awk '$1==s{print $2}' s=DayOfWeek file |
|
#4
|
|||
|
|||
|
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 < configfileThe trick is, read takes a variable name. So VAR=ABCD ; read $VAR reads into the ABCD variable. |
| Sponsored Links | |
|
|
#5
|
|||
|
|||
|
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
|
||||
|
||||
|
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
|
|||
|
|||
|
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 | ||
|
![]() |
| Tags |
| awk, config |
| Thread Tools | Search this Thread |
| 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 07:30 AM |
| parsing config file to create new config files | clazzic | Shell Programming and Scripting | 1 | 12-27-2009 02:06 PM |
| /etc/subsync parms definitions ? | Browser_ice | AIX | 0 | 12-16-2008 03:38 PM |
| SUN OS - UDP parms | nhatch | UNIX for Advanced & Expert Users | 1 | 09-05-2006 03:13 AM |
| Pulling out fields from a file | Saz | UNIX for Advanced & Expert Users | 2 | 09-30-2001 03:31 PM |
|
|