shell-base/modules/scripting/ini.mod

16 lines
454 B
Modula-2
Raw Normal View History

#!/bin/bash
# https://stackoverflow.com/a/61329094
parse_ini () {
local ini=$1
if [[ ! -f $ini ]]; then ini=$1.ini; if [[ ! -f $ini ]]; then echo no file "$ini"; return 1; fi fi
sed -n -E "/^\[.*\]/{s/\[(.*)\]/\1/;h;n;};/^[a-zA-Z]/{s/#.*//;G;s/([^ ]*) *= *(.*)\n(.*)/\3_\1='\2'/;p;}" "$ini" | \
sed -E 's/^_//'| \
sed -E 's/^([^=]*)[.]/\1_/' | \
sed -E 's/^([^=]*)[-]/\1_/' | \
( [[ $2 ]] && grep "$2" || cat )
}
ini2env () {
eval $(parse_ini "$@")
}