linux shell 处理ini文件

写ini配置

function write_ini_file()
{
    # 函数使用说明:
    #   write_ini_file <文件> <节> <键> <值>
    #   如果节或者键不存在,则添加
    #   如果节、键存在,值不匹配,则更新
    #   如果节、键、值均存在,则不做操作

    allSections=$(awk -F '[][]' '/\[.*]/{print $2}' $1)
    iniSections=(${allSections// /})
    # 判断是否要新建item
    itemFlag="0"
    for temp in ${iniSections[@]};do
        if [[ "${temp}" = "$2" ]];then
            itemFlag="1"
            break
        fi
    done

    if [[ "$itemFlag" = "0" ]];then
        echo "[$2]" >> $1
    fi

    # 加入或更新value
    awk "/\[$2\]/{a=1}a==1" $1|sed -e '1d' -e '/^$/d'  -e 's/[ \t]*$//g' -e 's/^[ \t]*//g' -e '/\[/,$d'|grep "$3.\?=">/dev/null
    if [[ "$?" -eq 0 ]];then
        # 更新
        # 找到指定item行号码
        itemNum=$(sed -n -e "/\[$2\]/=" $1)
        sed -i "${itemNum},/^\[.*\]/s/\($3.\?=\).*/\1$4/g" $1 >/dev/null 2>&1
        # 如果替换失败,可能文件中有\,更换分隔符为!
        if [[ "$?" -ne 0 ]];then
            sed -i "${itemNum},/^\[.*\]/s!\($3.\?=\).*!\1$4!g" $1
        fi
    else
        # 新增
        sed -i "/^\[$2\]/a\\$3=$4" $1
    fi
}

读ini配置

function read_value_from_ini()
{
    INIFILE=$1;
    SECTION=$2;
    ITEM=$3

    tmpfile="/tmp/tmp.ini"
    # 去掉配置文件中的注释行和空行,保存到tmpfile中。
    sed '/^#/d;/^$/d' ${INIFILE} > ${tmpfile}

    _readIni=`awk -F '=' '/\['${SECTION}'\]/{a=1}a==1&&$1~/'${ITEM}'/{print $2;exit}' ${tmpfile}`
    echo ${_readIni}
}
posted @ 2022-01-12 11:30  一只不会飞的猪  阅读(778)  评论(0)    收藏  举报