You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
121 lines
1.9 KiB
121 lines
1.9 KiB
#!/usr/bin/env bash
|
|
|
|
set -eu
|
|
|
|
ADD=0
|
|
|
|
synopsis() {
|
|
cat << EOF
|
|
Usage : `basename $0` [-h] [-a] system variable [value]
|
|
EOF
|
|
}
|
|
|
|
help() {
|
|
cat << EOF
|
|
|
|
`basename $0` affiche ou modifie une variable de configuration d'un système.
|
|
|
|
`synopsis`
|
|
|
|
Le fichier de configuration d'un système contient des définitions de variables.
|
|
|
|
Si l'argument value n'est pas précisé, cette commande affiche la valeur
|
|
de la variable.
|
|
|
|
Si l'argument value est précisé, cette commande fixe la valeur de la variable.
|
|
|
|
-h : affiche cette aide
|
|
-a : ajoute un espace puis la valeur précisée à la valeur courante.
|
|
|
|
|
|
EOF
|
|
}
|
|
|
|
usage() {
|
|
synopsis
|
|
exit 1
|
|
}
|
|
|
|
args() {
|
|
local opt
|
|
while getopts "ha" opt; do
|
|
case $opt in
|
|
h) help; exit 0;;
|
|
a) ADD=1;;
|
|
?) usage;;
|
|
esac
|
|
done
|
|
shift $(($OPTIND - 1))
|
|
if [ $# -ne 2 ] && [ $# -ne 3 ]; then
|
|
usage
|
|
fi
|
|
|
|
GUEST_NAME="$1"
|
|
VAR=$2
|
|
GET=1
|
|
[ $# = 3 ] && { GET=0; VALUE=$3; }
|
|
|
|
if echo $GUEST_NAME | grep -q '/'; then
|
|
error "$GUEST_NAME est un nom de système invalide"
|
|
fi
|
|
}
|
|
|
|
getValue() {
|
|
local file="$1" var="$2" v
|
|
v="`cat "$1" | grep "^$var=" | sed -ne 's/^[^=]*="\([^"]*\)".*$/\1/p'`"
|
|
[ -z "$v" ] && \
|
|
v="`cat "$1" | grep "^$var=" | sed -ne 's/^[^=]*=\([^ ]*\).*$/\1/p'`"
|
|
echo "$v"
|
|
}
|
|
|
|
|
|
|
|
setValue() {
|
|
local file="$1" var="$2" value="$3"
|
|
|
|
shift 2
|
|
|
|
cat $file | gawk -v value="$@" -e '
|
|
BEGIN {
|
|
found=0
|
|
}
|
|
|
|
/^'$var'=/ {
|
|
found=1
|
|
print "'$var'=\""value"\""
|
|
next
|
|
}
|
|
|
|
{ print }
|
|
|
|
END { if(found==0) print "'$var'=\""value"\"" }
|
|
' > $file.tmp
|
|
|
|
mv $file.tmp $file
|
|
}
|
|
|
|
|
|
# Programme principal
|
|
|
|
VDN_PATH=$(readlink -f $(dirname $0)/..); . $VDN_PATH/bin/functions.sh
|
|
|
|
args "$@"
|
|
|
|
|
|
GUEST_CONF=$NETWORK_DIR/$GUEST_NAME.conf
|
|
|
|
if [ $GET = 1 ]; then
|
|
GUEST_OWNER=$USER
|
|
loadGuestVars $GUEST_NAME
|
|
getValue $GUEST_CONF $VAR
|
|
else
|
|
if [ $ADD = 1 ]; then
|
|
GUEST_OWNER=$USER
|
|
loadGuestVars $GUEST_NAME
|
|
v=$(getValue $GUEST_CONF $VAR )
|
|
VALUE="$v $VALUE"
|
|
fi
|
|
setValue $GUEST_CONF $VAR "$VALUE"
|
|
fi
|
|
|