Results 1 to 6 of 6
What's the best way to read a value from a configuration file into a script?
I'm trying to get the command_file vaule from nagios.cfg into a script, this works but ...
- 06-08-2010 #1Linux Enthusiast
- Join Date
- Aug 2006
- Location
- Portsmouth, UK
- Posts
- 539
how to read a config variable into a script?
What's the best way to read a value from a configuration file into a script?
I'm trying to get the command_file vaule from nagios.cfg into a script, this works but what's the best way to do it?
Code:command_file=$(perl -F= -lane 'print $F[1] if /^[[:space:]]*command_file[[:space:]]*=/' /etc/nagios/nagios.cfg)
RHCE #100-015-395
Please don't PM me with questions as no reply may offend, that's what the forums are for.
- 06-08-2010 #2
if it works, why do you need a better way to do it?
is there a specific bottleneck you need to avoid?New to the internet, technical forums, or the hacker / open source community??
Read this to learn good posting habits http://www.catb.org/~esr/faqs/smart-questions.html
RHCE for RHEL version 5
RHCT for RHEL version 4
- 06-08-2010 #3Linux Enthusiast
- Join Date
- Aug 2006
- Location
- Portsmouth, UK
- Posts
- 539
There's never any harm in trying to improve your techniques or methods.
I'm also fairly sure that there is a better way to achieve this goal
RHCE #100-015-395
Please don't PM me with questions as no reply may offend, that's what the forums are for.
- 06-08-2010 #4
but what about it are you changing? does it take a long time to run? does it use too much ram? you need to be more specific.
anything that works is perfect unless there is a reason to improve it. There is no BEST way to do something. just a correct way and a wrong way.New to the internet, technical forums, or the hacker / open source community??
Read this to learn good posting habits http://www.catb.org/~esr/faqs/smart-questions.html
RHCE for RHEL version 5
RHCT for RHEL version 4
- 06-09-2010 #5
you could (if you can) change the config file to be a bash script and just invoke it.

config file:
script:Code:VAR="bla"
the combination should produce the outputCode:./config.cfg echo $VAR
Code:#$ ./script.sh bla
- 06-11-2010 #6
I often create config files using BASH variable syntax and then source them into the running script. This works because if you execute a config var script, it runs in a subshell process and the results are not propagated back to the parent process. But if you source it, you are adding it to your current process environment.
config file:
script file:Code:#!/bin/bash var1="some value" var2=42
Output:Code:#!/bin/bash $HOME/debug/junk.conf echo "var1 is $var1" echo "var2 is $var2" source $HOME/debug/junk.conf echo "var1 is $var1" echo "var2 is $var2"
Now if you have an existing config file that you cannot control the format of, pre-process it and produce an output file in /tmp that you can source. Then when you are done, delete your temp. In this way, you can massage the config file to remove unnecessary items or even change some using combinations of text processing commands (sed, cut, ...) and only source the bits that are needed.Code:var1 is var2 is var1 is some value var2 is 42


Reply With Quote