These two bash functions were previously written to modify and read an existing ini configuration file. Of course, they are actually implemented in Linux bash shell:
For exampleConfiguration file config. ini
# config.ininame = xiwangage = 27
Run the script and change name = xiwang to name = Xiaoqiang Wang (Eric). Expected output:
xiwang@ubuntu:~/Dev/BashConfig$ ./ConfigDemo.sh config.inicontents of config.ini----------------------------------------------# config.ininame = xiwangage = 27----------------------------------------------<CFG_NAME> is Xiaoqiang Wang(Eric)contents of config.ini----------------------------------------------# config.ininame = Xiaoqiang Wang(Eric)age = 27----------------------------------------------
Instance script:
#------------------------------------------------------------------------------# 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}WORKDIR=$PWDtest -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}
Update the shell script configuration file compatible with Inc. sh:
#!/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").txtcat <<EOF >$testfUNISERV_HOME=''export UNISERV_HOME=''EOFcat $testfcfgsh $testf UNISERV_HOME ABCDEFG/HIJ qecho '>>>Change>>to>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>'cat $testfcfgsh $testf UNISERV_HOME ~/Uniserv64 qqecho '>>>Change>>to>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>'cat $testfcfgsh $testf UNISERV_HOME ~/Uniserv64echo '>>>Change>>to>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>'cat $testfrm $testf
Test:
$ ~/migstuff/sh_template/wxConfigFiles.shUNISERV_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/Uniserv64export UNISERV_HOME=/home/xiwang/Uniserv64