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

16
init.sh
View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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