Created
January 24, 2024 11:37
-
-
Save ashish-kus/25bc670c44f6e5eda404019d70f7180d to your computer and use it in GitHub Desktop.
.ini config parser function written in bash for reading and exporting vafiable from config file.
This file contains 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
#!/usr/bin/env bash | |
parse_ini() { | |
local ini_file="$1" | |
# declare -A config | |
current_section="" | |
while IFS= read -r line; do | |
line=$(echo "$line" | sed -e 's/^[ \t]*//;s/[ \t]*$//') | |
if [[ "$line" =~ ^\; ]] || [[ -z "$line" ]]; then | |
continue | |
fi | |
if [[ "$line" =~ ^\[.*\]$ ]]; then | |
current_section=$(echo "$line" | sed -e 's/^\[\(.*\)\]$/\1/') | |
else | |
key=$(echo "$line" | cut -d '=' -f 1) | |
value=$(echo "$line" | cut -d '=' -f 2-) | |
echo "${current_section}_${key} = $value" | |
export "${current_section}_${key}"="$value" | |
fi | |
done < "$ini_file" | |
} | |
# Parse the INI file | |
parse_ini "$1" | |
# echo $SECTION1.KEY3 | |
echo $section_key |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment