bash 脚本实现的配置文件(ini/inc.sh)的读写操作

26 篇文章 0 订阅
25 篇文章 0 订阅

这两个bash 函数是以前写的,目的是对一个已经存在的ini配置文件进行修改和读取,当然实在Linux Bash Shell下进行的:

例如一个配置文件 config.ini

# config.ini
name = xiwang
age = 27
执行脚本,修改name=xiwang为name=Xiaoqiang Wang(Eric),期望的输出:

xiwang@ubuntu:~/Dev/BashConfig$ ./ConfigDemo.sh config.ini
contents of config.ini
----------------------------------------------
# config.ini
name = xiwang
age = 27

----------------------------------------------


<CFG_NAME> is Xiaoqiang Wang(Eric)

contents of config.ini
----------------------------------------------
# config.ini
name = Xiaoqiang Wang(Eric)
age = 27

----------------------------------------------


实例脚本:

#------------------------------------------------------------------------------
# model: cfg_get
# args: [1] => IN:<configure-file>
#       [2] => IN:<key>
#       [3] => OUT:<value-as-env-var>
# describe: get configure value by key from a configure file
# example:
#   > cfg_get "./Anubis.ini" "Anubis.ORB1.NSLocation" "OUT_MYVAR"
#   > echo $OUT_MYVAR
#   file:///etc/iors/ACNS_GlobalDev.ior
#------------------------------------------------------------------------------
# Usage: getcfg <in:file> <in:key> <out:valueENV>
getcfg() {
  [[ -f "$1" && ! -z "$2" && ! -z "$3" ]] || return 1
  export $3="$(cat "$1" | sed -n "/^$2/{ s~^[^=]*= \(.*\)$~\1~g; p; }" | tail -n 1)"
}

#------------------------------------------------------------------------------
# model: cfg_set
# args: [1] => IN:<configure-file>
#       [2] => IN:<key>
#       [3] => IN:<value>
# describe: set key = value to configure file
# example:
#   > cfg_set "./Anubis.ini" "Anubis.ORB1.NSLocation" "file:///etc/iors/ACNS_GlobalDev.ior"
#   > cat "./Anubis.ini"
#   Anubis.ORB1.NSLocation = file:///etc/iors/ACNS_GlobalDev.ior
#------------------------------------------------------------------------------
cfg_set()
{
  test -f "$1" && test ! -z "$2" && test ! -z "$3"
  if [ $? -eq 0 ]; then
    sed '/^'"$2"' =/{ s~^.*$~'"$2"' = '"$3"'~g }' -i "$1"
  fi
}

WORKDIR=$PWD

test -f "$1" && {
  echo "contents of config.ini"
  echo "----------------------------------------------"
  cat "$1"
  echo "----------------------------------------------"
  echo

  cfg_set "$1" "name" "Xiaoqiang Wang(Eric)"
  cfg_get "$1" "name" "CFG_NAME"
  echo
  echo "<CFG_NAME> is $CFG_NAME"
  echo

  echo "contents of config.ini"
  echo "----------------------------------------------"
  cat "$1"
  echo "----------------------------------------------"
  echo
}


更新兼容inc.sh的shell脚本配置文件:

#!/bin/bash
# File: wxConfigFiles.sh

#set -x

#------------------------------------------------------------------------------
# model: cfg_get
# args: [1] => IN:<configure-file>
#       [2] => IN:<key>
#       [3] => OUT:<value-as-env-var>
# describe: get configure value by key from a configure file
# example:
#   > cfg_get "./Anubis.ini" "Anubis.ORB1.NSLocation" "OUT_MYVAR"
#   > echo $OUT_MYVAR
#   file:///etc/iors/ACNS_GlobalDev.ior
#------------------------------------------------------------------------------
cfg_get()
{
  test -f "$1" && test ! -z "$2" && test ! -z "$3"
  if [ $? -eq 0 ]; then
    export $3="$(cat "$1" | sed -n '/^'"$2"'/{ s~^[^=]*= \(.*\)$~\1~g; p; }' | tail -n 1)"
  fi
}

#------------------------------------------------------------------------------
# model: cfg_set
# args: [1] => IN:<configure-file>
#       [2] => IN:<key>
#       [3] => IN:<value>
# describe: set key = value to configure file
# example:
#   > cfg_set "./Anubis.ini" "Anubis.ORB1.NSLocation" "file:///etc/iors/ACNS_GlobalDev.ior"
#   > cat "./Anubis.ini"
#   Anubis.ORB1.NSLocation = file:///etc/iors/ACNS_GlobalDev.ior
#------------------------------------------------------------------------------
cfg_set()
{
  test -f "$1" && test ! -z "$2" && test ! -z "$3"
  if [ $? -eq 0 ]; then
    sed '/^'"$2"' =/{ s~^.*$~'"$2"' = '"$3"'~g }' -i "$1"
  fi
}

# -----------------------------------------------------------------------------
# Configure a shell script
# args: [1] => In, shell configure file
#       [2] => In, Key
#       [3] => In, Value
#       [4]*=> In, (q)uote or double (qq)uote, or no quote if not set.
# -----------------------------------------------------------------------------
cfgsh()
{
  [ -f $1 ] && [ ! -z "$2" ] && [ ! -z "$3" ] && {
    case "$4" in
    q) q="'" ;;
    qq) q='"' ;;
    *) q="" ;;
    esac
    sed '/^\(export \)\{0,1\}'"$2"'=/{ s~^\(\(export\)\{0,1\}[^=]*\)=.*$~\1='"$q$3$q"'~g; }' -i "$1"
  }
}

testf=cfgsh_$(date +"%Y%m%d%H%M%S").txt
cat <<EOF >$testf
UNISERV_HOME=''
export UNISERV_HOME=''
EOF

cat $testf

cfgsh $testf UNISERV_HOME ABCDEFG/HIJ q
echo '>>>Change>>to>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>'
cat $testf

cfgsh $testf UNISERV_HOME ~/Uniserv64 qq
echo '>>>Change>>to>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>'
cat $testf

cfgsh $testf UNISERV_HOME ~/Uniserv64
echo '>>>Change>>to>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>'
cat $testf

rm $testf

测试:

$ ~/migstuff/sh_template/wxConfigFiles.sh
UNISERV_HOME=''
export UNISERV_HOME=''
>>>Change>>to>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
UNISERV_HOME='ABCDEFG/HIJ'
export UNISERV_HOME='ABCDEFG/HIJ'
>>>Change>>to>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
UNISERV_HOME="/home/xiwang/Uniserv64"
export UNISERV_HOME="/home/xiwang/Uniserv64"
>>>Change>>to>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
UNISERV_HOME=/home/xiwang/Uniserv64
export UNISERV_HOME=/home/xiwang/Uniserv64


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值