1

I was trying to setup an auto-update (for multiple servers) bash script via cron to a config file (text based) by downloading the new file everyday and replacing it directly. The script works fine, however there's a case where sometimes each server would need to have specific settings being different from the main config file, for example, one of the standard config line would be:

port_defined=1,2,3

meanwhile some servers would need:

port_defined=1,2,3,4,5,6

I can manually change that config line on the corresponding server, however it would get overwritten again everytime the auto-update script being run.

So the question, can I make a single bash script to download the reference config file, and then compare it first with the local config file (on the corresponding server) for any differences and then merge it?

By "merge" I mean combining the line properly into:

port_defined=1,2,3,4,5,6

rather than two separate lines:

port_defined=1,2,3
port_defined=1,2,3,4,5,6
1

Do you really mean to merge the lists of values? That is

port_defined=1,3,5
port_defined=2,4,123

should be combined into

port_defined=1,2,3,4,5,123

If so, you could try

BEGIN { FS = "=" }
NR == FNR && /ports_defined/ { ports=$2 }
NR > FNR {
    if ($1 == "ports_defined") {
        ports = ports "," $2
        split(ports, p, ",")
        for (i in p) {
            if (!num[p[i]]) {
                result = result "," p[i]
                num[p[i]] = 1
            }
        }
        print $1 "=" substr(result, 2)
    } else {
        print
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.