Avoir le contenu suivant dans un fichier:
VARIABLE1="Value1"
VARIABLE2="Value2"
VARIABLE3="Value3"
J'ai besoin d'un script qui affiche les éléments suivants:
Content of VARIABLE1 is Value1
Content of VARIABLE2 is Value2
Content of VARIABLE3 is Value3
Des idées?
awk -F\= '{gsub(/"/,"",$2);print "Content of " $1 " is " $2}' <filename>
Just FYI, une autre solution pure bash
IFS="="
while read -r name value
do
echo "Content of $name is ${value//\"/}"
done < filename
Comme votre fichier de configuration est un script Shell valide, vous pouvez le créer dans votre Shell actuel:
. config_file
echo "Content of VARIABLE1 is $VARIABLE1"
echo "Content of VARIABLE2 is $VARIABLE2"
echo "Content of VARIABLE3 is $VARIABLE3"
Un peu plus sec, mais plus délicat
. config_file
for var in VARIABLE1 VARIABLE2 VARIABLE3; do
echo "Content of $var is ${!var}"
done
Si vous avez besoin de ces ...
=
(ie var = value
n'échouera pas);shopt -s extglob
configfile="dos_or_unix" # set the actual path name of your (DOS or Unix) config file
tr -d '\r' < $configfile > $configfile.unix
while IFS='= ' read -r lhs rhs
do
if [[ ! $lhs =~ ^\ *# && -n $lhs ]]; then
rhs="${rhs%%\#*}" # Del in line right comments
rhs="${rhs%%*( )}" # Del trailing spaces
rhs="${rhs%\"*}" # Del opening string quotes
rhs="${rhs#\"*}" # Del closing string quotes
declare $lhs="$rhs"
fi
done < $configfile.unix
tr -d '\r' ...
supprime le retour chariot DOS.! $lhs =~ ^\ *#
ignore les commentaires sur une seule ligne et -n $lhs
ignore les lignes vides.
Pour effacer les espaces de fin avec ${rhs%%*( )}
, vous devez paramétrer une étendue étendue avec shopt -s extglob
. (En utilisant sed
), vous pouvez éviter cela en passant par le plus complexe:
rhs="${rhs%"${rhs##*[^ ]}"}"
## This is a comment
var1=value1 # Right side comment
var2 = value2 # Assignment with spaces
## You can use blank lines
var3= Unquoted String # Outer spaces trimmed
var4= "My name is " # Quote to avoid trimming
var5= "\"Bob\""
echo "Content of var1 is $var1"
echo "Content of var2 is $var2"
echo "Content of var3 is [$var3]"
echo "Content of var4 + var5 is: [$var4$var5]"
Content of var1 is value1
Content of var2 is value2
Content of var3 is [Unquoted String]
Content of var4 + var5 is: [My name is "Bob"]
Je fais de cette façon
. $PATH_TO_FILE
awk '{print "Content of "$1" is "$3}' FS='[="]'
Résultat
Le contenu de VARIABLE1 est Value1 Le contenu de VARIABLE2 est Value2 Le contenu de VARIABLE3 est Value3
donné un fichier de configuration comme suit: -
[a]
b=C
d=E;rm t1
[b]
g=h
la ligne suivante analysera et conservera les valeurs: -
CFG=path-to-file; for ini in `awk '/^\[/' $CFG`;do unset ARHG;declare -A ARHG;while read A B;do ARHG[$A]=$B;echo "in section $ini, $A is equal to" ${ARHG["$A"]};done < <(awk -F'=' '/\[/ {x=0} x==1 && $0~/=/ && NF==2 {print $1, $2} $0==INI {x=1}' INI="$ini" $CFG);declare -p ARHG;echo;done;printf "end loop\n\n";declare -p ARHG
Maintenant, décomposons cela
CFG=path-to-file;
for ini in `awk '/^\[/' $CFG` # finds the SECTIONS (aka "ini")
do
unset ARHG # resets ARHG
declare -A ARHG # declares an associative array
while read A B
do
ARHG[$A]=$B
echo "in section $ini, $A is equal to" ${ARHG["$A"]}
done < <(awk -F'=' '/\[/ {x=0} x==1 && $0~/=/ && NF==2 {print $1, $2} $0==INI {x=1}' INI="$ini" $CFG)
# the awk splits the file into sections,
# and returns pairs of values separated by "="
declare -p ARHG # displays the current contents of ARHG
echo
done
printf "end loop\n\n"
declare -p ARHG
Cela nous permet de sauvegarder des valeurs, sans utiliser eval ou backtick. Pour être "vraiment propre", nous pourrions supprimer [: space:] au début et à la fin de la ligne, ignorer les lignes "^ #" et supprimer les espaces autour du signe "égal"
#
#------------------------------------------------------------------------------
# parse the ini like $0.$Host_name.cnf and set the variables
# cleans the unneeded during after run-time stuff. Note the MainSection
# courtesy of : http://mark.aufflick.com/blog/2007/11/08/parsing-ini-files-with-sed
#------------------------------------------------------------------------------
doParseConfFile(){
# set a default cnfiguration file
cnf_file="$run_unit_bash_dir/$run_unit.cnf"
# however if there is a Host dependant cnf file override it
test -f "$run_unit_bash_dir/$run_unit.$Host_name.cnf" \
&& cnf_file="$run_unit_bash_dir/$run_unit.$Host_name.cnf"
# yet finally override if passed as argument to this function
# if the the ini file is not passed define the default Host independant ini file
test -z "$1" || cnf_file=$1;shift 1;
test -z "$2" || ini_section=$2;shift 1;
doLog "DEBUG read configuration file : $cnf_file"
doLog "INFO read [$ini_section] section from config file"
# debug echo "@doParseConfFile cnf_file:: $cnf_file"
# coud be later on parametrized ...
test -z "$ini_section" && ini_section='MAIN_SETTINGS'
doLog "DEBUG reading: the following configuration file"
doLog "DEBUG ""$cnf_file"
( set -o posix ; set ) | sort >"$tmp_dir/vars.before"
eval `sed -e 's/[[:space:]]*\=[[:space:]]*/=/g' \
-e 's/#.*$//' \
-e 's/[[:space:]]*$//' \
-e 's/^[[:space:]]*//' \
-e "s/^\(.*\)=\([^\"']*\)$/\1=\"\2\"/" \
< $cnf_file \
| sed -n -e "/^\[$ini_section\]/,/^\s*\[/{/^[^#].*\=.*/p;}"`
( set -o posix ; set ) | sort >"$tmp_dir/vars.after"
doLog "INFO added the following vars from section: [$ini_section]"
cmd="$(comm -3 $tmp_dir/vars.before $tmp_dir/vars.after | Perl -ne 's#\s+##g;print "\n $_ "' )"
echo -e "$cmd"
echo -e "$cmd" >> $log_file
echo -e "\n\n"
sleep 1; printf "\033[2J";printf "\033[0;0H" # and clear the screen
}
#eof func doParseConfFile