Fixes in resolve_alias() & resolve_targets()

This commit is contained in:
Patrick Van der Veken 2021-06-16 22:25:43 +02:00
parent c8297aaad2
commit 71eb0acc02

View File

@ -43,7 +43,7 @@
# or LOCAL_CONFIG_FILE instead # or LOCAL_CONFIG_FILE instead
# define the version (YYYY-MM-DD) # define the version (YYYY-MM-DD)
typeset -r SCRIPT_VERSION="2023-03-24" typeset -r SCRIPT_VERSION="2021-06-17"
# name of the global configuration file (script) # name of the global configuration file (script)
typeset -r GLOBAL_CONFIG_FILE="manage_ssh.conf" typeset -r GLOBAL_CONFIG_FILE="manage_ssh.conf"
# name of the local configuration file (script) # name of the local configuration file (script)
@ -427,7 +427,7 @@ fi
if (( DO_SSH_AGENT )) if (( DO_SSH_AGENT ))
then then
# ssh-agent # ssh-agent
which ssh-agent >/dev/null 2>/dev/null command -v ssh-agent >/dev/null 2>/dev/null
# shellcheck disable=SC2181 # shellcheck disable=SC2181
if (( $? > 0 )) if (( $? > 0 ))
then then
@ -1153,7 +1153,7 @@ typeset RECURSION_COUNT=$2
typeset ALIASES_LINE="" typeset ALIASES_LINE=""
typeset ALIAS_LIST="" typeset ALIAS_LIST=""
typeset ALIAS="" typeset ALIAS=""
typeset IS_ALIAS=0 typeset IS_GROUP=0
typeset EXPANDED_ALIASES="" typeset EXPANDED_ALIASES=""
# check MAX_RECURSION to avoid segmentation faults # check MAX_RECURSION to avoid segmentation faults
@ -1181,11 +1181,11 @@ fi
# expand alias line into individual aliases # expand alias line into individual aliases
# do not use variable substition (>=ksh93) instead use the uglier while loop solution # do not use variable substition (>=ksh93) instead use the uglier while loop solution
# (hint: don't use a for loop as aliases may contain spaces!) # (hint: don't use a for loop as aliases may contain spaces!)
print "${ALIASES_LINE}"| while IFS=',' read -r ALIAS print "${ALIASES_LINE}" | tr -s ',' '\n' 2>/dev/null | while read -r ALIAS
do do
# recurse if the alias is a group # recurse if the alias is a group
IS_ALIAS=$(print "${ALIAS}" | grep -c -E -e '^\@' 2>/dev/null) IS_GROUP=$(print "${ALIAS}" | grep -c -E -e '^\@' 2>/dev/null)
if (( IS_ALIAS > 0 )) if (( IS_GROUP > 0 ))
then then
RECURSION_COUNT=$(( RECURSION_COUNT + 1 )) RECURSION_COUNT=$(( RECURSION_COUNT + 1 ))
EXPANDED_ALIASES=$(resolve_alias "${ALIAS}" ${RECURSION_COUNT}) EXPANDED_ALIASES=$(resolve_alias "${ALIAS}" ${RECURSION_COUNT})
@ -1197,7 +1197,8 @@ do
then then
ALIAS_LIST="${EXPANDED_ALIASES}" ALIAS_LIST="${EXPANDED_ALIASES}"
else else
ALIAS_LIST="${ALIAS_LIST}\n${EXPANDED_ALIASES}" ALIAS_LIST="${ALIAS_LIST}
${EXPANDED_ALIASES}"
fi fi
# if the recursion fails, return the current output list # if the recursion fails, return the current output list
# and go back up into the recursion tree # and go back up into the recursion tree
@ -1211,7 +1212,8 @@ do
then then
ALIAS_LIST="${ALIAS}" ALIAS_LIST="${ALIAS}"
else else
ALIAS_LIST="${ALIAS_LIST}\n${ALIAS}" ALIAS_LIST="${ALIAS_LIST}
${ALIAS}"
fi fi
fi fi
done done
@ -1240,13 +1242,13 @@ function resolve_targets
typeset TARGETS_LIST="" typeset TARGETS_LIST=""
typeset EXPANDED_TARGETS="" typeset EXPANDED_TARGETS=""
typeset TARGET="" typeset TARGET=""
typeset IS_TARGET=0 typeset IS_GROUP=0
grep -v -E -e '^#' -e '^$' "${TARGETS_FILE}" 2>/dev/null | while read -r TARGET grep -v -E -e '^#' -e '^$' "${TARGETS_FILE}" 2>/dev/null | while read -r TARGET
do do
# resolve group target # resolve group target
IS_TARGET=$(print "${TARGET}" | grep -c -E -e '^\@' 2>/dev/null) IS_GROUP=$(print "${TARGET}" | grep -c -E -e '^\@' 2>/dev/null)
if (( IS_TARGET > 0 )) if (( IS_GROUP > 0 ))
then then
EXPANDED_TARGETS=$(resolve_alias "${TARGET}" 0) EXPANDED_TARGETS=$(resolve_alias "${TARGET}" 0)
# shellcheck disable=SC2181 # shellcheck disable=SC2181
@ -1256,7 +1258,8 @@ do
then then
TARGETS_LIST="${EXPANDED_TARGETS}" TARGETS_LIST="${EXPANDED_TARGETS}"
else else
TARGETS_LIST="${TARGETS_LIST}\n${EXPANDED_TARGETS}" TARGETS_LIST="${TARGETS_LIST}
${EXPANDED_TARGETS}"
fi fi
fi fi
# add individual target # add individual target
@ -1265,7 +1268,8 @@ do
then then
TARGETS_LIST="${TARGET}" TARGETS_LIST="${TARGET}"
else else
TARGETS_LIST="${TARGETS_LIST}\n${TARGET}" TARGETS_LIST="${TARGETS_LIST}
${TARGET}"
fi fi
fi fi
done done