Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1814668
  • 博文数量: 274
  • 博客积分: 2366
  • 博客等级: 大尉
  • 技术积分: 1880
  • 用 户 组: 普通用户
  • 注册时间: 2007-04-22 09:37
文章分类

全部博文(274)

文章存档

2022年(1)

2020年(10)

2019年(7)

2018年(18)

2017年(26)

2016年(32)

2015年(43)

2014年(30)

2013年(44)

2012年(36)

2011年(17)

2010年(10)

分类: LINUX

2015-09-18 16:29:11

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

例如一个配置文件 config.ini


[plain] view plaincopy
  1. # config.ini  
  2. name = xiwang  
  3. age = 27  
执行脚本,修改name=xiwang为name=Xiaoqiang Wang(Eric),期望的输出:



  1. xiwang@ubuntu:~/Dev/BashConfig$ ./ConfigDemo.sh config.ini  
  2. contents of config.ini  
  3. ----------------------------------------------  
  4. # config.ini  
  5. name = xiwang  
  6. age = 27  
  7.   
  8. ----------------------------------------------  
  9.   
  10.   
  11. <CFG_NAME> is Xiaoqiang Wang(Eric)  
  12.   
  13. contents of config.ini  
  14. ----------------------------------------------  
  15. # config.ini  
  16. name = Xiaoqiang Wang(Eric)  
  17. age = 27  
  18.   
  19. ----------------------------------------------  



实例脚本:


  1. #------------------------------------------------------------------------------  
  2. # model: cfg_get  
  3. # args: [1] => IN:<configure-file>  
  4. #       [2] => IN:<key>  
  5. #       [3] => OUT:<value-as-env-var>  
  6. # describe: get configure value by key from a configure file  
  7. # example:  
  8. #   > cfg_get "./Anubis.ini" "Anubis.ORB1.NSLocation" "OUT_MYVAR"  
  9. #   > echo $OUT_MYVAR  
  10. #   file:///etc/iors/ACNS_GlobalDev.ior  
  11. #------------------------------------------------------------------------------  
  12. # Usage: getcfg <in:file> <in:key> <out:valueENV>  
  13. getcfg() {  
  14.   [[ -f "$1" && ! -z "$2" && ! -z "$3" ]] || return 1  
  15.   export $3="$(cat "$1" | sed -n "/^$2/{ s~^[^=]*= .?$~\1~g; p; }" | tail -n 1)"  
  16. }  
  17.   
  18. #------------------------------------------------------------------------------  
  19. # model: cfg_set  
  20. # args: [1] => IN:<configure-file>  
  21. #       [2] => IN:<key>  
  22. #       [3] => IN:<value>  
  23. # describe: set key = value to configure file  
  24. # example:  
  25. #   > cfg_set "./Anubis.ini" "Anubis.ORB1.NSLocation" "file:///etc/iors/ACNS_GlobalDev.ior"  
  26. #   > cat "./Anubis.ini"  
  27. #   Anubis.ORB1.NSLocation = file:///etc/iors/ACNS_GlobalDev.ior  
  28. #------------------------------------------------------------------------------  
  29. cfg_set()  
  30. {  
  31.   test -f "$1" && test ! -z "$2" && test ! -z "$3"  
  32.   if [ $? -eq 0 ]; then  
  33.     sed '/^'"$2"' =/{ s~^.*$~'"$2"' = '"$3"'~g }' -i "$1"  
  34.   fi  
  35. }  
  36.   
  37. WORKDIR=$PWD  
  38.   
  39. test -f "$1" && {  
  40.   echo "contents of config.ini"  
  41.   echo "----------------------------------------------"  
  42.   cat "$1"  
  43.   echo "----------------------------------------------"  
  44.   echo  
  45.   
  46.   cfg_set "$1" "name" "Xiaoqiang Wang(Eric)"  
  47.   cfg_get "$1" "name" "CFG_NAME"  
  48.   echo  
  49.   echo "<CFG_NAME> is $CFG_NAME"  
  50.   echo  
  51.   
  52.   echo "contents of config.ini"  
  53.   echo "----------------------------------------------"  
  54.   cat "$1"  
  55.   echo "----------------------------------------------"  
  56.   echo  
  57. }  


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


  1. #!/bin/bash  
  2. # File: wxConfigFiles.sh  
  3.   
  4. #set -x  
  5.   
  6. #------------------------------------------------------------------------------  
  7. # model: cfg_get  
  8. # args: [1] => IN:<configure-file>  
  9. #       [2] => IN:<key>  
  10. #       [3] => OUT:<value-as-env-var>  
  11. # describe: get configure value by key from a configure file  
  12. # example:  
  13. #   > cfg_get "./Anubis.ini" "Anubis.ORB1.NSLocation" "OUT_MYVAR"  
  14. #   > echo $OUT_MYVAR  
  15. #   file:///etc/iors/ACNS_GlobalDev.ior  
  16. #------------------------------------------------------------------------------  
  17. cfg_get()  
  18. {  
  19.   test -f "$1" && test ! -z "$2" && test ! -z "$3"  
  20.   if [ $? -eq 0 ]; then  
  21.     export $3="$(cat "$1" | sed -n '/^'"$2"'/{ s~^[^=]*= .?$~\1~g; p; }' | tail -n 1)"  
  22.   fi  
  23. }  
  24.   
  25. #------------------------------------------------------------------------------  
  26. # model: cfg_set  
  27. # args: [1] => IN:<configure-file>  
  28. #       [2] => IN:<key>  
  29. #       [3] => IN:<value>  
  30. # describe: set key = value to configure file  
  31. # example:  
  32. #   > cfg_set "./Anubis.ini" "Anubis.ORB1.NSLocation" "file:///etc/iors/ACNS_GlobalDev.ior"  
  33. #   > cat "./Anubis.ini"  
  34. #   Anubis.ORB1.NSLocation = file:///etc/iors/ACNS_GlobalDev.ior  
  35. #------------------------------------------------------------------------------  
  36. cfg_set()  
  37. {  
  38.   test -f "$1" && test ! -z "$2" && test ! -z "$3"  
  39.   if [ $? -eq 0 ]; then  
  40.     sed '/^'"$2"' =/{ s~^.*$~'"$2"' = '"$3"'~g }' -i "$1"  
  41.   fi  
  42. }  
  43.   
  44. # -----------------------------------------------------------------------------  
  45. # Configure a shell script  
  46. # args: [1] => In, shell configure file  
  47. #       [2] => In, Key  
  48. #       [3] => In, Value  
  49. #       [4]*=> In, (q)uote or double (qq)uote, or no quote if not set.  
  50. # -----------------------------------------------------------------------------  
  51. cfgsh()  
  52. {  
  53.   [ -f $1 ] && [ ! -z "$2" ] && [ ! -z "$3" ] && {  
  54.     case "$4" in  
  55.     q) q="'" ;;  
  56.     qq) q='"' ;;  
  57.     *) q="" ;;  
  58.     esac  
  59.     sed '/^export\{0,1\}'"$2"'=/{ s~^\(export\{0,1\}[^=]*\)=.*$~\1='"$q$3$q"'~g; }' -i "$1"  
  60.   }  
  61. }  
  62.   
  63. testf=cfgsh_$(date +"%Y%m%d%H%M%S").txt  
  64. cat <<EOF >$testf  
  65. UNISERV_HOME=''  
  66. export UNISERV_HOME=''  
  67. EOF  
  68.   
  69. cat $testf  
  70.   
  71. cfgsh $testf UNISERV_HOME ABCDEFG/HIJ q  
  72. echo '>>>Change>>to>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>'  
  73. cat $testf  
  74.   
  75. cfgsh $testf UNISERV_HOME ~/Uniserv64 qq  
  76. echo '>>>Change>>to>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>'  
  77. cat $testf  
  78.   
  79. cfgsh $testf UNISERV_HOME ~/Uniserv64  
  80. echo '>>>Change>>to>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>'  
  81. cat $testf  
  82.   
  83. rm $testf  

测试:



  1. $ ~/migstuff/sh_template/wxConfigFiles.sh  
  2. UNISERV_HOME=''  
  3. export UNISERV_HOME=''  
  4. >>>Change>>to>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>  
  5. UNISERV_HOME='ABCDEFG/HIJ'  
  6. export UNISERV_HOME='ABCDEFG/HIJ'  
  7. >>>Change>>to>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>  
  8. UNISERV_HOME="/home/xiwang/Uniserv64"  
  9. export UNISERV_HOME="/home/xiwang/Uniserv64"  
  10. >>>Change>>to>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>  
  11. UNISERV_HOME=/home/xiwang/Uniserv64  
  12. export UNISERV_HOME=/home/xiwang/Uniserv64  
阅读(1232) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~