Re: leggere .conf files da bash
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Ho fatto qualche funzione per files di configurazioni con stanze... se
a qualcuno possono essere utili:
# Function: get_stanzas
# Purpose : Print the list of stanzas found in $1 file
get_stanzas()
{
typeset config_file=$1
awk -F '[][]' '
NF==3 && $0 ~ /^\[.*\]/ { print $2 }
' ${config_file}
}
# Function: set_var_in_stanza
# Purpose : read the stanza from config file and set al parameters as
variable
set_vars_in_stanza()
{
typeset cfg_file=$1
typeset cur_stanza=$2
eval `sed -e 's/[[:space:]]*\=[[:space:]]*/=/g' \
-e 's/#.*$//' \
-e 's/[[:space:]]*$//' \
-e 's/^[[:space:]]*//' \
-e "s/^\(.*\)=\([^\"']*\)$/\1=\"\2\"/" \
< $cfg_file \
| sed -n -e "/^\[$cur_stanza\]/,/^\s*\[/{/^[^;].*\=.*/p;}"`
}
# Function: unset_var_in_stanza
# Purpose : read the stanza from config file and free all parameters
previously set as variables
free_vars_in_stanza()
{
typeset cfg_file=$1
typeset cur_stanza=$2
vars_in_stanzas=$(sed -n
"/^\[$cur_stanza\]/,/^\s*\[/{/^[^#].*\=.*/p;}" < $cfg_file | grep -o
^[^=]*)
for var in $vars_in_stanzas ; do
#eval "echo $var=\$$var"
unset $var
done
}
Ciao
Piviul
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
iEYEARECAAYFAkvVqEMACgkQ+pTAvRvvoB2oZACeMTfl5Z1wc1A2J2f/RV7qtzkq
byQAnjtyUe2Nb6yi+w8/sZvdWt5b9i5p
=88z0
-----END PGP SIGNATURE-----
Reply to: