继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

Shell script - Linux下解析ini配置文件

PIPIONE
关注TA
已关注
手记 921
粉丝 147
获赞 701

导语

  • Linux 有时候需要统计多台主机上面的数据,比如合并N多主机的日志,然后进行下一步的分析。这个时候如果直接把所有主机IP写死到脚本中的话,下次新增一台主机,就需要再去修改脚本,如果涉及到多个脚本的话,这样的操作将会大大浪费我们的时间

  • 这个时候我们可以考虑把用到的主机列表配置到一个文件中,然后通过一个函数来解析它,以后修改就只用修改这个配置文件就好了

  • 当然处理封装到公共的脚本中外,我们也可以将其封装成一个单独的命令,详见最后部分 作为定制化工具


公用函数脚本

listIniSections

Desc:
      列举配置文件中所有的section名称
Usage:
      listIniSections ini-config-file

listIniKeys

Desc:
      列举配置文件中 给定的section 下所有的key名称
Usage:
      listIniSections ini-config-file section-name

listIniValues

Desc:
      列举配置文件中 给定的section 下所有的value名称
Usage:
      listIniSections ini-config-file section-name

listIniKeysValues

Desc:
      列举配置文件中 给定的section 下所有的key 和 value名称
Usage:
      listIniSections ini-config-file section-name

脚本

## 定义一个公用的脚本,把常见的函数放到里面,别的脚本就可以引用然后调用## 类似于面向对象语言的包含文件vim /devOps/shell/common/functions#!/usr/bin/env bash #### 2016-05-12## 得到ini配置文件所有的sections名称## listIniSections "filename.ini"##listIniSections()
{
    inifile="$1"
    # echo "inifile:${inifile}"
    # # exit
    if [ $# -ne 1 ] || [ ! -f ${inifile} ]    then
        echo  "file [${inifile}] not exist!"
        exit 
    else
        sections=`sed -n '/\[*\]/p' ${inifile}  |grep -v '^#'|tr -d []`        echo  "${sections}"
    fi}#### 2016-05-12## 得到ini配置文件给定section的所有key值## ini中多个section用空行隔开## listIniSections "filename.ini" "section"## listIniKeys()
{
    inifile="$1"
    section="$2"
    if [ $# -ne 2 ] || [ ! -f ${inifile} ]    then
        echo  "ini file not exist!"
        exit 
    else
        keys=$(sed -n '/\['$section'\]/,/^$/p' $inifile|grep -Ev '\[|\]|^$'|awk -F'=' '{print $1}')        echo ${keys}
    fi}#### 2016-05-12## 得到ini配置文件给定section的所有value值## ini中多个section用空行隔开## listIniSections "filename.ini" "section"## listIniValues()
{
    inifile="$1"
    section="$2"
    if [ $# -ne 2 ] || [ ! -f ${inifile} ]    then
        echo "ini file [${inifile}]!"
        exit 
    else
        values=$(sed -n '/\['$section'\]/,/^$/p' $inifile|grep -Ev '\[|\]|^$'|awk -F'=' '{print $2}')        echo ${values}
    fi}## 2016-06-01## 得到ini配置文件给定section的所有key - value值## ini中多个section用空行隔开## listIniSections "filename.ini" "section"## listIniKeysValues()
{
    inifile="$1"
    section="$2"
    if [ $# -ne 2 ] || [ ! -f ${inifile} ]    then
        echo "ini file [${inifile}]!"
        exit 
    else
        values=$(sed -n '/\['$section'\]/,/^$/p' $inifile|grep -Ev '\[|\]|^$'|awk -F'=' '{print $1, $2}')        echo ${values}
    fi}

具体实例

ini格式配置文件

root@pts/2 $ cat hostList.ini 
[juepei]
web1=192.168.88.2web2=192.168.88.15[miaowu]
web1=192.168.200.2web2=192.168.200.3

测试脚本

root@pts/0 $ cat 20160620.sh 
#!/usr/bin/env bash 
source /devOps/shell/common/functionsiniconfig="/tmp/liuchao/hostList.ini"echo "list all sections"listIniSections ${iniconfig}echo -e "\nlist section [juepei] keys"listIniKeys ${iniconfig} juepeiecho -e "\nlist section [juepei] values"listIniValues ${iniconfig} juepeiecho -e "\nlist section [miaowu] keys and values"listIniKeysValues ${iniconfig} juepei

测试结果

root@pts/0 $ bash 20160620.sh 
list all sections
juepei
miaowulist section [juepei] keys
web1 web2list section [juepei] values192.168.88.2 192.168.88.15list section [miaowu] keys and values
web1 192.168.88.2 web2 192.168.88.15

<a name="md-anchor" id="md-anchor">作为定制化工具</a>

#!/usr/bin/env bash# -*- coding: utf-8 -*-#Filename:  lc_parseINI#Author:        Liuchao#Email:     137642091@qq.com#Date:      2016-06-20#Desc:      Linux下解析INI 格式的配置文件#source /devOps/shell/common/functionsecho "$1 - $2 - $3"## 如果没有指定参数或者给出的参数大于2则输入用法if [ $# -gt 3 ] || [ $# -lt 1 ]then 
    echo "Usage:`basename $0` [-s/-k/-v/-a] iniconfig [section]"
    exitelse
    # param1="$1"
    # if [ $# -eq 1 ] && [ "${param1:0-3}" = "ini" ]
    if [ $# -eq 1 ] && [ "${1:0-3}" = "ini" ]    then
        listIniSections "$1"
    elif [ $# -eq 2 ] && [ "$1" = "-s" ]    then
        listIniSections "$2"
    elif [ $# -eq 3 ] && [ "$1" = "-s" ]    then
        listIniSections "$2"
    elif [ $# -eq 3 ] && [ "$1" = "-a" ]    then
        listIniKeysValues "$2" "$3"
    elif [ $# -eq 3 ] && [ "$1" = "-k" ]    then
        listIniKeys "$2" "$3"
    elif [ $# -eq 3 ] && [ "$1" = "-v" ]    then
        listIniValues "$2" "$3"
    else
        echo "You enter wrong params!"
        echo "Usage:`basename $0` [-s/-k/-v/-a] iniconfig section"
        exit
    fifi



作者:全栈运维
链接:https://www.jianshu.com/p/a9a93f92084b

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP