code cleanup: force local var before calling a for loop, make sure it's unset when no longer needed

This commit is contained in:
fatalerrors
2021-10-06 10:37:13 +02:00
parent c0b1bf1665
commit 1ac3ec303c
9 changed files with 65 additions and 16 deletions

View File

@@ -51,6 +51,7 @@ export SYS_ARCH=$(uname -m)
for lib in $MYPATH/lib/*.sh; do for lib in $MYPATH/lib/*.sh; do
. $lib . $lib
done done
unset lib
# ============================= # =============================
# ==== Basic sanity checks ==== # ==== Basic sanity checks ====
@@ -133,6 +134,7 @@ fi
for mod in $MODULE_LIST; do for mod in $MODULE_LIST; do
. modules/$mod.sh . modules/$mod.sh
done done
unset mod
if [[ $RUN_SHELL == true ]]; then if [[ $RUN_SHELL == true ]]; then
prnt I "Lancement d'un shell intéractif..." prnt I "Lancement d'un shell intéractif..."
@@ -142,11 +144,14 @@ if [[ $RUN_SHELL == true ]]; then
fi fi
# Run prechecks # Run prechecks
[[ JUMP != true ]] && for mod in $MODULE_LIST; do if [[ JUMP != true ]]; then
for mod in $MODULE_LIST; do
version=VER_$mod version=VER_$mod
prnt I "Vérification initiale pour $mod version ${!version}..." prnt I "Vérification initiale pour $mod version ${!version}..."
precheck_$mod precheck_$mod
done done
unset mod
fi
# If we only checks, we stop here # If we only checks, we stop here
if [[ $CHECK_ONLY == true ]]; then if [[ $CHECK_ONLY == true ]]; then
@@ -186,6 +191,7 @@ if [[ $key == "C" || $key == 'c' ]]; then
$mod $mod
echo $mod >> $STAGE_FILE # Mark as done for resuming function echo $mod >> $STAGE_FILE # Mark as done for resuming function
done done
unset mod
echo echo
else else
echo -e "${Yellow}Le système n'a subit aucune modifications.${DEFAULTCOL}" echo -e "${Yellow}Le système n'a subit aucune modifications.${DEFAULTCOL}"

View File

@@ -26,6 +26,7 @@ function die()
else else
prnt W "On continue malgrés l'erreur #$errorcode." prnt W "On continue malgrés l'erreur #$errorcode."
fi fi
unset errorcode
# Put the trigger back (only executed with --keepgoing) # Put the trigger back (only executed with --keepgoing)
trap "error ${LINENO}" ERR trap "error ${LINENO}" ERR
@@ -61,6 +62,7 @@ function error()
else else
prnt E "Erreur ligne $(caller), avec le code d'erreur $code." prnt E "Erreur ligne $(caller), avec le code d'erreur $code."
fi fi
unset parent_lineno message code
} }
# Trigger error function on error # Trigger error function on error
trap "error ${LINENO}; backtrace; err_exit" ERR trap "error ${LINENO}; backtrace; err_exit" ERR
@@ -82,14 +84,16 @@ function backtrace
{ {
echo "======== Pile d'appel ========" echo "======== Pile d'appel ========"
typeset -i i=0 typeset -i i=0
for func in "${FUNCNAME[@]}"
do local func=
for func in "${FUNCNAME[@]}"; do
if [[ $i -ne 0 ]]; then if [[ $i -ne 0 ]]; then
printf '%15s() %s:%d\n' \ printf '%15s() %s:%d\n' \
"$func" "${BASH_SOURCE[$i]}" "${BASH_LINENO[ (( $i - 1)) ]}" "$func" "${BASH_SOURCE[$i]}" "${BASH_LINENO[ (( $i - 1)) ]}"
fi fi
let i++ || true let i++ || true
done done
unset func i
echo "==============================" echo "=============================="
} }
@@ -111,8 +115,7 @@ noerror()
else else
$@ $@
fi fi
local code=$? echo $?
echo $code
trap "error ${LINENO}" ERR trap "error ${LINENO}" ERR
set -o errexit set -o errexit

View File

@@ -110,6 +110,8 @@ prnt() {
;; ;;
esac esac
echo -e "${IWhite}$(date $DATEFORMAT)${DEFAULTFG} ${heads} $@" echo -e "${IWhite}$(date $DATEFORMAT)${DEFAULTFG} ${heads} $@"
unset heads
} }
export -f prnt export -f prnt

View File

@@ -28,6 +28,7 @@ backupdist()
exit 11 exit 11
fi fi
local file=
for file in $@; do for file in $@; do
local tmstmp=$(stdtime) local tmstmp=$(stdtime)
if [[ - L ${file} ]]; then if [[ - L ${file} ]]; then
@@ -50,7 +51,9 @@ backupdist()
else else
prnt W "backupdist(): $file n'existe pas, rien à faire." prnt W "backupdist(): $file n'existe pas, rien à faire."
fi fi
unset tmstmp
done done
unset file
} }
export -f backupdist export -f backupdist
@@ -72,6 +75,7 @@ installfile()
die 7 die 7
fi fi
local arg=
for arg in $@; do for arg in $@; do
if [[ -f $MYPATH/repo/hosts/$HOSTNAME/$arg ]]; then if [[ -f $MYPATH/repo/hosts/$HOSTNAME/$arg ]]; then
filelist="$filelist $MYPATH/repo/hosts/$HOSTNAME/$arg" filelist="$filelist $MYPATH/repo/hosts/$HOSTNAME/$arg"
@@ -82,15 +86,19 @@ installfile()
filelist="$filelist $arg" filelist="$filelist $arg"
fi fi
done done
unset arg
# Empty to just obtain the target which is the last element of the list # Empty to just obtain the target which is the last element of the list
for i in $filelist; do local file=
for file in $filelist; do
: :
done done
if [[ ! $i == /* ]]; then if [[ ! $file == /* ]]; then
prnt E "installfile(): Target must be on the root filesystem and full path must be provided." prnt E "installfile(): Target must be on the root filesystem and full path must be provided."
die 13 die 13
fi fi
unset file
if [[ -d $(dirname $i) ]]; then if [[ -d $(dirname $i) ]]; then
prnt I "Création du répertoire $(dirname $i) d'accueil..." prnt I "Création du répertoire $(dirname $i) d'accueil..."
mkdir -pv $(dirname $i) mkdir -pv $(dirname $i)

View File

@@ -31,9 +31,11 @@ pkginst()
if [[ ! $INSTALL_MODE == dev ]]; then if [[ ! $INSTALL_MODE == dev ]]; then
$PKG_MAN $COM_INSTALL $@ $PKG_MAN $COM_INSTALL $@
else else
local pkg=
for pkg in $@; do for pkg in $@; do
$PKG_MAN $COM_INSTALL $pkg $PKG_MAN $COM_INSTALL $pkg
done done
unset pkg
fi fi
} }
export -f pkginst export -f pkginst
@@ -61,9 +63,11 @@ pkgrem()
if [[ ! $INSTALL_MODE == dev ]]; then if [[ ! $INSTALL_MODE == dev ]]; then
$PKG_MAN $COM_REMOVE $@ $PKG_MAN $COM_REMOVE $@
else else
local pkg=
for pkg in $@; do for pkg in $@; do
$PKG_MAN $COM_REMOVE $pkg $PKG_MAN $COM_REMOVE $pkg
done done
uset pkg
fi fi
} }
export -f pkgrem export -f pkgrem

View File

@@ -25,8 +25,12 @@ exec_serv()
local lineexec=$(echo $INIT_COM | local lineexec=$(echo $INIT_COM |
sed -e s/%srv%/$svcname/ \ sed -e s/%srv%/$svcname/ \
-e s/%com%/$command/) -e s/%com%/$command/)
unset svcname command
prnt I "Lancement de la commande $command du services $svcname" prnt I "Lancement de la commande $command du services $svcname"
$lineexec $lineexec
unset lineexec
} }
export exec_serv export exec_serv
@@ -35,9 +39,11 @@ export exec_serv
# Start one or more service # Start one or more service
svc_start() svc_start()
{ {
local svc=
for svc in $@; do for svc in $@; do
exec_serv $svc start exec_serv $svc start
done done
unset svc
} }
export -f svc_start export -f svc_start
@@ -46,9 +52,11 @@ export -f svc_start
# Restart one or more services # Restart one or more services
svc_restart() svc_restart()
{ {
local svc=
for svc in $@; do for svc in $@; do
exec_serv $svc restart exec_serv $svc restart
done done
unset svc
} }
export -f svc_restart export -f svc_restart
@@ -57,9 +65,11 @@ export -f svc_restart
# Stop one or more services # Stop one or more services
svc_stop() svc_stop()
{ {
local svc=
for svc in $@; do for svc in $@; do
exec_serv $svc stop exec_serv $svc stop
done done
unset svc
} }
export -f svc_stop export -f svc_stop

View File

@@ -76,12 +76,19 @@ show_version()
echo -e "${BWhite}init.sh${DEFAULTCOL} version ${BGreen}$VERSION${DEFAULTCOL}" echo -e "${BWhite}init.sh${DEFAULTCOL} version ${BGreen}$VERSION${DEFAULTCOL}"
echo "" echo ""
echo "Modules disponibles :" echo "Modules disponibles :"
local mod=
for mod in $MYPATH/modules/*.sh; do for mod in $MYPATH/modules/*.sh; do
. $mod . $mod
local modname=$(get_mod_name $mod) local modname=$(get_mod_name $mod)
local version=VER_$modname local version=VER_$modname
echo -e "\t$BWhite$modname${DEFAULTCOL}: $BGreen${!version}$DEFAULTCOL" echo -e "\t$BWhite$modname${DEFAULTCOL}: $BGreen${!version}$DEFAULTCOL"
unset modname version
done done
unset mod
if [[ $(id -u) -ne 0 ]]; then if [[ $(id -u) -ne 0 ]]; then
echo -e "\n${BYellow}Attention :$DEFAULTCOL Ce script requiert les droits d'administration pour fonctionner." echo -e "\n${BYellow}Attention :$DEFAULTCOL Ce script requiert les droits d'administration pour fonctionner."
fi fi
@@ -98,4 +105,3 @@ get_mod_name()
export -f get_mod_name export -f get_mod_name
# EOF # EOF

View File

@@ -61,3 +61,5 @@ get_os_version()
fi fi
} }
export -f get_os_version export -f get_os_version
# EOF

View File

@@ -25,6 +25,7 @@ read_commandline()
local want_logfile=false local want_logfile=false
local want_conffile=false local want_conffile=false
local opt=
for opt in $@; do for opt in $@; do
case $opt in case $opt in
"-h"|"--help") "-h"|"--help")
@@ -105,12 +106,15 @@ read_commandline()
;; ;;
esac esac
done done
unset opt
# If those var are true at that point, something is wrong # If those var are true at that point, something is wrong
if [[ $want_logfile == true ]] || [[ $want_module == true ]] || if [[ $want_logfile == true ]] || [[ $want_module == true ]] ||
[[ $want_conffile == true ]]; then [[ $want_conffile == true ]]; then
syntax_error syntax_error
fi fi
unset want_conffile want_logfile want_module
} }
export -f read_commandline export -f read_commandline
@@ -150,6 +154,8 @@ load_autoconf()
if [[ -n $SYS_CODE && -e $prefix/$SYS_DIST-$SYS_CODE-$SYS_ARCH.conf.sh ]]; then if [[ -n $SYS_CODE && -e $prefix/$SYS_DIST-$SYS_CODE-$SYS_ARCH.conf.sh ]]; then
. $prefix/$SYS_DIST-$SYS_CODE-$SYS_ARCH.conf.sh . $prefix/$SYS_DIST-$SYS_CODE-$SYS_ARCH.conf.sh
fi fi
unset prefix
} }
export -f load_autoconf export -f load_autoconf
@@ -162,6 +168,7 @@ export -f load_autoconf
load_configuration() load_configuration()
{ {
if [[ -n $CONFFILES ]]; then if [[ -n $CONFFILES ]]; then
local f=
for f in $CONFFILES; do for f in $CONFFILES; do
prnt I "Chargement de $f spécifié manuellement." prnt I "Chargement de $f spécifié manuellement."
if [[ -s $f ]]; then if [[ -s $f ]]; then
@@ -171,6 +178,7 @@ load_configuration()
die 6 --force die 6 --force
fi fi
done done
unset f
else else
prnt I "Chargement de la configuration..." prnt I "Chargement de la configuration..."
if [[ -e $MYPATH/conf/$HOSTNAME.conf.sh ]]; then if [[ -e $MYPATH/conf/$HOSTNAME.conf.sh ]]; then