Find the answer to your Linux question:
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 ...
  1. #1
    Linux Enthusiast
    Join Date
    Aug 2006
    Location
    Portsmouth, UK
    Posts
    539

    Question 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.

  2. #2
    Linux Enthusiast meton_magis's Avatar
    Join Date
    Oct 2006
    Location
    arizona
    Posts
    665
    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

  3. #3
    Linux 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.

  4. #4
    Linux Enthusiast meton_magis's Avatar
    Join Date
    Oct 2006
    Location
    arizona
    Posts
    665
    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

  5. #5
    Linux Enthusiast Kloschüssel's Avatar
    Join Date
    Oct 2005
    Location
    Italy
    Posts
    717
    you could (if you can) change the config file to be a bash script and just invoke it.

    config file:
    Code:
    VAR="bla"
    script:
    Code:
    ./config.cfg
    echo $VAR
    the combination should produce the output
    Code:
    #$ ./script.sh
    bla

  6. #6
    Just Joined! sixdrift's Avatar
    Join Date
    Jan 2007
    Location
    In and around and about Cary, NC
    Posts
    44
    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:
    Code:
    #!/bin/bash
    var1="some value"
    var2=42
    script file:
    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"
    Output:
    Code:
    var1 is 
    var2 is 
    var1 is some value
    var2 is 42
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...