Sunday, 15 September 2013

linux - Shell script to migrate config file variables -



linux - Shell script to migrate config file variables -

i need way migrate variables 1 config file next/newer file during update script on embedded scheme using shell scripting.

i have config file structured read cgi-scripts , c programs:

[version] api_version = 10 [broadcast] ip = 239.10.10.10 port = 35680 [transmission] uuid_long = yes

during update process, need take new file structured, , re-create values older file. values exist in old file exist in new, not fields in new file exist in old.

my thought loop through file looking key = value\n pattern, , performing replacement in new file, i'm not sure how grep strip 2 values can use, , sed match against key , new line character without knowing value in new file.

note: have 'sh' work with, no bash, if makes difference. (embedded system)

thank suggestion utilize awk. weak @ shell scripting, if there improvements can made please advise.

#!/bin/sh # collect keys original config file keys=$(awk '{ if ($1 !~ /\[[a-za-z]*\]/ && $1 != "") print $1, "\n" }' /etc/file.conf) # loop through keys replacing values in new config file key in $keys value=$(awk -v key="$key" '{ if($1 ~ key) print $3 }' /etc/file.conf) sed -i "s/$key.*/$key = $value/g" /etc/new.file.conf done

linux bash shell sh

No comments:

Post a Comment