参考stackoverflow的例子,改了一个出来:
while IFS='= ' read var val
do
if [[ $var == \[*] ]]
then
section=$(echo $var | sed 's/^\[\(.*\)\]$/\1/')
elif [[ $val ]]
then
if [ -z $section ];then
declare "${var}=$val"
else
declare "${section}.${var}=$val"
fi
fi
done < config.ini
使用的时候:
${section.key}
就可以读到变量啦。