linux - Add or update a configuration record in /etc/environment -
my /etc/environment looks this:
cat /etc/environment path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games"
i wish use command (sed, awk, python, whatever....) make this:
cat /etc/environment path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games" java_home="/usr/lib/jvm/java-6-sun"
now catch is, rather 1 liner (in fields of sed -xyz /domagic/ /etc/environment), needs contain merging logic - either appends new configuration record or update existing one. bottom line, should prevent file looking this: (caused in experienced shell scripters calling echo >> on each invocation)
cat /etc/environment path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games" java_home="/usr/lib/jvm/java-5-sun" java_home="/usr/lib/jvm/java-6-sun" java_home="/usr/lib/jvm/java-6-sun" java_home="/usr/lib/jvm/java-6-sun"
i guess trick questions, because i'm trying avoid using custom scripts, such as
/usr/local/bin/propupdate /etc/environment java_home "/usr/lib/jvm/java-6-sun"
/usr/local/bin/propupdate following script (written sake of example, may contain bugs. comments appreciated)
#!/bin/bash # append/update configuration record in file # # usage example: # /usr/local/bin/propupdate /etc/environment java_home "/usr/lib/jvm/java-6-sun" # # author maxim veksler <maxim@vekslers.org> # version 0.5-2010-07-27 expected_args=3 e_badargs=3 e_badfile=4 if [[ $# -ne ${expected_args} ]]; echo "usage: `basename $0` /path/to/config.conf parametername newvaluetext" >&2 exit $e_badargs fi configuration_file="$1" configuration_parameter="$2" configuration_value="$3" if [[ ! -e "${configuration_file}" ]]; echo "configuration file ${configuration_file} not exist" >&2 exit $e_badfile fi if [[ ! -w "${configuration_file}" ]]; echo "can't modify ${configuration_file}" >&2 exit $e_badfile fi ######################################### ## decide parameter adding ## ######################################### __param_found=0 # first check configuration_parameter supplied use contains "=" if [[ ${configuration_parameter} == *=* ]]; # should exist in file, plain if grep -qe "^${configuration_parameter}" "${configuration_file}"; __param_found=1 suffix_regex='[[:space:]]*' fi else # ok, sophisticated user, did not send "=" parameter... if grep -qe "^${configuration_parameter}[[:space:]]*=" "${configuration_file}"; # let's check if such configuration parameter + "=" exists __param_found=1 suffix_regex='[[:space:]]*=[[:space:]]*' elif grep -qe "^${configuration_parameter}[[:space:]]+" "${configuration_file}"; # if such parameter exists, @ __param_found=1 suffix_regex='[[:space:]]\+' fi fi if [[ $__param_found == 1 ]]; #echo sed -i "s|^\(${configuration_parameter}${suffix_regex}\).*$|\1${configuration_value}|g" "${configuration_file}" sed -i "s|^\(${configuration_parameter}${suffix_regex}\).*$|\1${configuration_value}|g" "${configuration_file}" else if [[ ${configuration_parameter} == *=* ]]; # configuration parameter contains "=" in it's name, append echo "${configuration_parameter}${configuration_value}" >> "${configuration_file}" else # try guess if file "param = value" or "param value" type of file. if grep -qe "^[[:alnum:]]+[[:space:]]*=" "${configuration_file}"; # seems "param = value" type of file echo "${configuration_parameter}=${configuration_value}" >> "${configuration_file}" else # seems "param value" type of file echo "${configuration_parameter} ${configuration_value}" >> "${configuration_file}" fi fi fi #cat $configuration_file
thank you, maxim.
-- update: kinda liked script, i've improved bit. seems production ready. enjoy.
instead of trying parse /etc/environment
file, instead create file own name in /etc/profile.d/
, described in my answer relevant question. copy on during installation, because contains content. let alone make scripts shorter.
Comments
Post a Comment