Created
December 21, 2024 19:06
-
-
Save sulincix/a7a97a899ec45db6b303518f34017838 to your computer and use it in GitHub Desktop.
posix shell ini parser without command
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
parse(){ | |
section="$1" | |
value="$2" | |
found="false" | |
while read line ; do | |
case $line in | |
[*) | |
if [ "$line" == "[$section]" ] ; then | |
found="true" | |
fi | |
;; | |
"$value"=*) | |
if [ "$found" == "true" ] ; then | |
ret=${line#*=} | |
echo $(eval echo $ret) | |
return | |
fi | |
esac | |
done < /dev/stdin | |
} | |
test(){ | |
echo "[main]" | |
echo "name=\"main\"" | |
echo "number=31" | |
echo "[test]" | |
echo "name=\"test\"" | |
echo "number=13" | |
} | |
parse "main" "name" < <(test) | |
parse "main" "number" < <(test) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment