Shell + perl : Ini file parser
#!/bin/ksh
ini_parser()
{
# Check if ini file exists
if [ ! -f $1 ]; then
echo "Erreur: le fichier a parser n'existe pas"
return 0
fi
# Rewrite ini file in shell script
perl -lane 'if($_ =~ /^$/){$s="";next;}; if(m/^\[([^\]]+)\]$/g) { $s=$1;$s=~s/[^a-z0-9_]/_/g }; print $s.($s eq ""?"":"_").$_ if ($_ !~ m/^(\[|;)/ and $_ =~ m/^[^=]+=[^=]+/);print $s."=".$_, $s="" if($_ !~ m/^(\[|;)/ and $_ !~ m/^[^=]+=[^=]+/)' $1 >"${1}.tmp"
# Call tmp shell script in bash
. "${1}.tmp"
# Remove tmp shell script after loading
rm "${1}.tmp"
}
Written by Ke20
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Perl
Authors
janosgyerik
25.16K
Jean-Remy Duboc
12.25K
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#