linux Shell 读取和写入ini配置文件

awk -F '=' "/\[${section}\]/{a=1}a==1" ${iniFile}|sed -e '1d' -e '/^$/d' -e '/^\[.*\]/,$d' -e "/^${option}=.*/!d" -e "s/^${option}=//"
# awk 找出出 section 之后的内容
# sed 条件1:去除第一行 条件2:去除空行 条件3:去除其他section的内容 条件4:去除不匹配${key}=的行 条件5:将${key}=字符剔除

  

#!/usr/bin/bash
getLine(){
    file=$1
    section=$2
    option=$3
    tl=$(cat $file | wc -l)
    lines=$(sed -n -e "/$section/=" $file)
    tn=0
    lindedata=''
    for i in $lines
    do
        nlines=$(cat $file | awk -v i=$i '/^\[.*\]$/ {if(NR>i)print NR}')
        nline=$(echo $nlines | awk '{print $1}')
        if [ -n $nline ]
        then
            #echo '-----------------------------------------'
            #echo $i $nline
            sl=$((i+1))
            el=$((nline-1))
            if [ $el -eq -1 ]
            then
                el=$tl
            fi
            #echo $sl $el
            if [ $((tl-nline)) -ne 0 ]
            then
                rs=$(cat $file | head -n $el | tail -n "+"$sl | grep -n '^'$option | tail -n 1)
                #echo $rs
                px=$(echo $rs | awk -F ':' '{print $1}')
                dd=$(echo $rs | awk -F ':' '{print $2}')
                flag=$(echo $dd | awk '{if("'$dd'"==""){print 0}else{print 1}}')
                #echo $flag
                if [ $flag -eq 1 ]
                then
                    tn=$((i+px))
                    linedata=$dd
                fi
            fi
        fi
        #echo $nline
    done
    return $tn

}
getConfig(){
    file=$1
    section=$2
    option=$3
    getLine $file $section $option
    line=$?
    echo $line
    content=$(cat $file | awk '{if(NR=="'$line'"){print}}')
    echo $content


}
getConfig test.conf comon files

  

猜你喜欢

转载自www.cnblogs.com/navysummer/p/12117958.html