Search code examples
bashawksedreadlineconfiguration-files

Bash - Read config files and make changes in this file


i have config file like this for example:

# Blah blah, this is sample config file blah blah
# Something more blah blah
value1=YES
value2=something
#value3=boom
# Blah blah
valueN=4145

And i want to make script to read and edit config files like this. I thinking about make a menu with groups of config options, then after write an option console output will be like this:

Group of funny options (pick option to change value):
1. value1=YES
2. value2=something
3. [disabled]value3=boom

After picking 1 for exaple i can change value1 from YES to NO or disable and activate other (hash unhash) plus adding new variables to the end of file. Then in the end save all changes in this config file. Any tips what i need to use? Actually trying with read line + awk to skip # lines (with space), but still i have problem to get all this variables and making changes in config file. I will be grateful for your help.

Edit.

while read line 
do 
echo $line | awk '$1' != "#" && / / { print $1 $3 }' 
done < config.conf 

Thinking about this for now to read informations what i want. Plus i'm gonna use something like this to change values:

sed -c -i "s/("one" *= *).*/\1$two/" config.conf 

I have completly no idea how i can get this variables to my script and use it like i write before. Actually i search for any tips, not someone who write this script for me. I'm beginner at linux scripting :V


Solution

  • I would recommend to abstain from such an, seemingly generic configuration program, because the comments might contain important informations about the current value and will be outdated, if the values change, while the comments don't.

    Second problem is, that I would expect, if activating an entry is possible, deactivating it should be possible too. So now you have 2 options what to do with each value.

    Third problem: In most cases, guessing a type by the value might work. YES seems to be a boolean, 47 an int, foobar a name - or is it a file? - but often a wider type is possible too, so YES can be just a string or a file, 47.3 might be valid where 47 is or might be not and so on.

    However, for experimenting and trying things out, select and grep might be a start:

    select line in $(grep "=" sample.conf) "write" "abort"
    do
        case $line in
            "write") echo write; break ;;
            "abort") echo abort; break ;;
            '#'*=*) echo activate $line;;
            *=[0-9]*) echo int value $line;;
            *=YES|NO) echo boolean value $line;;
            *) echo text value $line ;;
        esac
    done
    

    Instead of 'echo intvalue $line' you would probably call a function "intconfigure" where only int values are accepted. For "write", you would write back to the file, but I omitted, conserving the comments without assignment and sorting them in again at the right position has to be done, which isn't trivial, given the opportunity to activate or deactivate comments.

    But read up on the select command in shell and try it out and see how far you come.

    If you think you have reached a usable solution, use this for all your configuration files privately and see, whether you prefer it over using a simple editor or not.