这两个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