socle_bash/lib/path.lib

110 lines
3.7 KiB
Plaintext
Raw Normal View History

2017-07-21 18:44:49 +11:00
##----------------------------------------------------------------------------
## Script : path.lib
## Module Puppet : gnc-script_core_dtsi
## Auteur : Emmanuel Confrere
## Date : 10-02-2016
## Version : 2.0.0
## Objet : Ajout d'un repertoire a une variable avec control d unicite.
##
## Fonction : fctpath
##
## detail : Certaines variables represente une liste de repertoire
## separes par un caractere significatif. C'est le cas pour
## les variables PATH, MANPATH, etc...
## Cette fonction permet la mise a jour de ce type de
## variable sans se poser la question de la redondance
## de l'information ou de l'existance du repertoire.
## L'appel de cette fonction doit etre suivi de quatre
## parametres :
## - 1 : le nom du repertoire a ajouter
## - 2 : le nom de la variable a mettre a jour
## - 3 : le separateur de champs utilise
## - 4 : le parametre "avant" si vous desirez ajouter
## le repertoire en debut de variable
## La variable est automatiquement exportee.
# ----------------------------------------------------------------------------
# Mise a jour :
# 1.0.0 19-08-2001 - Marc GRESSET - Creation
# 1.0.0 10-02-2016 - Emmanuel Confrere - Integration au script du socle DTSI
##---------------------------------------------------------------------------
fctpath () {
local V_PATH_NAME="${1}"
local V_PATH_VAR="${2}"
local V_PATH_SEPAR="${3}"
local V_PATH_WHERE="${4}"
eval V_PATH_CONTENU=\"\$\{"${V_PATH_VAR}"\}\"
#- Suppression des caracteres V_PATH_SEPAR au debut et en fin de ligne
V_PATH_CONTENU=`echo "${V_PATH_CONTENU}"|sed "s#^${V_PATH_SEPAR}##;s#${V_PATH_SEPAR}\\$##"`
if [ -z "${V_PATH_NAME}" ] || [ -z "${V_PATH_VAR}" ] || [ -z "${V_PATH_SEPAR}" ]
then
echo "fct_path : Probleme sur les parametres :"
echo "fct_path : \${V_PATH_NAME} = [ ${V_PATH_NAME} ]"
echo "fct_path : \${V_PATH_VAR} = [ ${V_PATH_VAR} ]"
echo "fct_path : \${V_PATH_SEPAR} = [ ${V_PATH_SEPAR} ]"
return 1
else
case ${V_PATH_SEPAR} in
\.|\,|\;|\:|\!|\~|\@|\#|\%|\^) \
:
;;
*) echo "fct_path : Probleme sur le separateur de champs."
echo "fct_path : \${V_PATH_NAME} = [ ${V_PATH_NAME} ]"
echo "fct_path : \${V_PATH_VAR} = [ ${V_PATH_VAR} ]"
echo "fct_path : \${V_PATH_SEPAR} = [ ${V_PATH_SEPAR} ]"
return 2
;;
esac
fi
# - Suppression du path si existe deja dans la variable
case ${V_PATH_CONTENU} in
*${V_PATH_SEPAR}${V_PATH_NAME}${V_PATH_SEPAR}* ) \
V_PATH_CONTENU=`echo ${V_PATH_CONTENU}|sed "s#${V_PATH_SEPAR}${V_PATH_NAME}${V_PATH_SEPAR}#${V_PATH_SEPAR}#"` ;;
${V_PATH_NAME}${V_PATH_SEPAR}* ) \
V_PATH_CONTENU=`echo ${V_PATH_CONTENU}|sed "s#${V_PATH_NAME}${V_PATH_SEPAR}##"` ;;
*${V_PATH_SEPAR}${V_PATH_NAME} ) \
V_PATH_CONTENU=`echo ${V_PATH_CONTENU}|sed "s#${V_PATH_SEPAR}${V_PATH_NAME}##"` ;;
${V_PATH_NAME} ) V_PATH_CONTENU="" ;;
esac
# - Initialisation de la nouvelle variable
if [ "${V_PATH_WHERE}" != "supp" ]
then
if [ -z "${V_PATH_CONTENU}" ]
then
V_PATH_CONTENU="${V_PATH_NAME}"
elif [ "${V_PATH_WHERE}" = "avant" ]
then
V_PATH_CONTENU=${V_PATH_NAME}${V_PATH_SEPAR}${V_PATH_CONTENU}
else
V_PATH_CONTENU=${V_PATH_CONTENU}${V_PATH_SEPAR}${V_PATH_NAME}
fi
fi
if [ ! -d "${V_PATH_NAME}" ] && [ ! -f "${V_PATH_NAME}" ]
then
#- Le repertoire specifier n existe pas
unset V_PATH_NAME
unset V_PATH_VAR
unset V_PATH_SEPAR
unset V_PATH_WHERE
unset V_PATH_CONTENU
return 2
else
#- Export du nouveau Path
eval ${V_PATH_VAR}=${V_PATH_CONTENU}
eval export ${V_PATH_VAR}
unset V_PATH_NAME
unset V_PATH_VAR
unset V_PATH_SEPAR
unset V_PATH_WHERE
unset V_PATH_CONTENU
fi
}
typeset -Ffx fctpath