linuxShell读取和写⼊ini配置⽂件(脚本实现读取和写⼊,附
使⽤⽅法和讲解)
原始代码中有awk和sed理解起来⽐较费劲:
【⽹上博客都有读取,没有写⼊】
⽽且,读取功能为
`awk -F '=' '/‘$Section’/{a=1}a==1&&$1~/'$Key'/{print $2;exit}' $Configfile
有BUG 当 读取【Item0】中的 【newf】  正确应该返回【空】 但是 按照这个写法会跳到后⾯去,会返回【Item1】的【newf】
修正后
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}=字符剔除
正⽂:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
保存下来直接可以使⽤:
读取:
source dealIni.sh iniFile section option
参数  iniFile :读取全sections    保存在环境变量中为 shell数组 iniSections
source dealIni.sh iniFile
参数  iniFile section:读取全section下全options (option=value)    保存在环境变量中为 shell数组 iniOptions
source dealIni.sh iniFile section
巴西龟
参数  iniFile section option:读取全section中 option的 value  保存在环境变量中为 shell数组 iniValue source dealIni.sh iniFile section option
写⼊:
参数  -w(标记为写⼊) iniFile section option value
source dealIni.sh -w iniFile section option value
#该脚本必须⽤ source 命令⽽且结果获取为${var}获取,不是return 如:source readIni.sh 否则变量⽆法外传
# dealIni.sh iniFile section option
# read
# param : iniFile section option    return value --- a str    use: ${iniValue}
# param : iniFile section          return options (element: option = value) --- a str[]  use: arr_length=${#iniOptions[*]}}  element=${iniOptions[0]} # param : iniFile                  returm sections (element: section ) --- a str[]  use: arr_length=${#iniSections[*]}}  element=${iniSections[0]}
# write
#param : -w iniFile section option value  add new element:section option    result:if not --->creat ,have--->update,exist--->do nothing牛仔很忙 歌词
#option ,value can not be null
#params
iniFile=$1
section=$2
option=$3
#sun
mode="iniR"
echo $@ | grep "\-w" >/dev/null&&mode="iniW"
if [ "$#" = "5" ]&&[ "$mode" = "iniW" ];then
iniFile=$2
section=$3
option=$4
value=$5
#echo $iniFile $section $option $value
fi
#resullt
iniValue='default'
iniOptions=()
iniSections=()
function checkFile()
function checkFile()
{
if [ "${iniFile}" = ""  ] || [ ! -f ${iniFile} ];then
echo "[error]:file --${iniFile}-- not exist!"
fi
}
function readInIfile()
{
if [ "${section}" = "" ];then
#通过如下两条命令可以解析成⼀个数组
allSections=$(awk -F '[][]' '/\[.*]/{print $2}' ${iniFile})
iniSections=(${allSections// /})
echo "[info]:iniSections size:-${#iniSections[@]}- eles:-${iniSections[@]}- "
elif [ "${section}" != "" ] && [ "${option}" = "" ];then
#判断section是否存在读取配置文件失败
allSections=$(awk -F '[][]' '/\[.*]/{print $2}' ${iniFile})
echo $allSections|grep ${section}
if [ "$?" = "1" ];then
echo "[error]:section --${section}-- not exist!"
return 0
fi
#正式获取options
#a=(获取匹配到的section之后部分|去除第⼀⾏|去除空⾏|去除每⼀⾏⾏⾸⾏尾空格|将⾏内空格变为@G@(后⾯分割时为数组时,空格会导致误拆))        a=$(awk "/\[${section}\]/{a=1}a==1"  ${iniFile}|sed -e'1d' -e '/^$/d'  -e 's/[ \t]*$//g' -e 's/^[ \t]*//g' -e 's/[ ]/@G@/g' -e '/\[/,$d' )
b=(${a})
for i in ${b[@]};do
#剔除⾮法字符,转换@G@为空格并添加到数组尾
if [ -n "${i}" ]||[ "${i}" i!= "@G@" ];then
iniOptions[${#iniOptions[@]}]=${i//@G@/ }
fi
done
echo "[info]:iniOptions size:-${#iniOptions[@]}- eles:-${iniOptions[@]}-"
elif [ "${section}" != "" ] && [ "${option}" != "" ];then
# iniValue=`awk -F '=' '/\['${section}'\]/{a=1}a==1&&$1~/'${option}'/{print $2;exit}' $iniFile|sed -e 's/^[ \t]*//g' -e 's/[ \t]*$//g'`
iniValue=`awk -F '=' "/\[${section}\]/{a=1}a==1" ${iniFile}|sed -e '1d' -e '/^$/d' -e '/^\[.*\]/,$d' -e "/^${option}.*=.*/!d" -e "s/^${option}.*= *//"`
echo "[info]:iniValue value:-${iniValue}-"
新闻的特点是什么
fi
}
function writeInifile()
{
#检查⽂件
checkFile
allSections=$(awk -F '[][]' '/\[.*]/{print $2}' ${iniFile})
iniSections=(${allSections// /})
#判断是否要新建section
sectionFlag="0"
for temp in ${iniSections[@]};do
if [ "${temp}" = "${section}" ];then
sectionFlag="1"
break
fi
done
if [ "$sectionFlag" = "0" ];then
echo "[${section}]" >>${iniFile}
fi
#加⼊或更新value
awk "/\[${section}\]/{a=1}a==1" ${iniFile}|sed -e '1d' -e '/^$/d'  -e 's/[ \t]*$//g' -e 's/^[ \t]*//g' -e '/\[/,$d'|grep "${option}.\?=">/dev/null
if [ "$?" = "0" ];then
#更新
#到制定section⾏号码
小学班主任个人总结sectionNum=$(sed -n -e "/\[${section}\]/=" ${iniFile})
sed -i "${sectionNum},/^\[.*\]/s/\(${option}.\?=\).*/\1 ${value}/g" ${iniFile}
sed -i "${sectionNum},/^\[.*\]/s/\(${option}.\?=\).*/\1 ${value}/g" ${iniFile}        echo "[success] update [$iniFile][$section][$option][$value]"
else
#新增
#echo sed -i "/^\[${section}\]/a\\${option}=${value}" ${iniFile}
sed -i "/^\[${section}\]/a\\${option} = ${value}" ${iniFile}
echo "[success] add [$iniFile][$section][$option][$value]"
fi
}
#main
if [ "${mode}" = "iniR" ];then
checkFile
readInIfile
elif [ "${mode}" = "iniW" ];then
writeInifile
fi