..

对上一段shell脚本的更新

修改后的代码如下1

#!/bin/bash
CONF=$1
set_key_value() {
  local key=${1}
  local value=${2}
  if [ -n $value ]; then
    local current=$(sed -n -e "s/^\($key = \)\([^ \"]*\)\(.*\)$/\2/p" $CONF)
    value="$(echo "${value}" | sed 's|[&]|\\&|g')"

    # 数值
    if [ -n "$current" ];then
      echo "setting $CONF : $key = $value"
      sed -i "s|^[#]*[ ]*${key}\([ ]*\)=.*|${key} = ${value}|" ${CONF} 
    else  # 字符串
      echo "setting $CONF : $key = $value"
      sed -i "s|^[#]*[ ]*${key}\([ ]*\)=.*|${key} = \"${value}\"|" ${CONF}
    fi
  fi
}

set_key_value timeout_propose "4s"
set_key_value timeout_xxx true