support/scripts/mkusers: fix shellcheck errors

the user tables do not use trailing backslash \ to continue lines,
so we don't want them to be interpreted thusly, so we use 'read -r'
(SC2162).

Integer variables need not be quoted (SC2086). In any case, should
there be an actual issue and they be set empty, that would cause a
runtime issue, wether they be quoted or not.

The binary -o and -a ar perfectly defined in bash's test (SC2166).

Signed-off-by: Yann E. MORIN <yann.morin.1998@free.fr>
(cherry picked from commit 01b3053cec)
Signed-off-by: Peter Korsgaard <peter@korsgaard.com>
This commit is contained in:
Yann E. MORIN 2024-03-10 00:44:26 +01:00 committed by Peter Korsgaard
parent 9698702615
commit 1f46f56346
2 changed files with 21 additions and 7 deletions

View File

@ -1466,7 +1466,6 @@ support/scripts/expunge-gconv-modules Shellcheck
support/scripts/fix-configure-powerpc64.sh EmptyLastLine
support/scripts/generate-gitlab-ci-yml Shellcheck
support/scripts/mkmakefile ConsecutiveEmptyLines Shellcheck
support/scripts/mkusers Shellcheck
support/scripts/setlocalversion Shellcheck
support/testing/tests/core/post-build.sh Shellcheck
support/testing/tests/package/test_opkg/post-build.sh Shellcheck

View File

@ -26,6 +26,7 @@ error() {
shift
printf "%s: " "${myname}" >&2
# shellcheck disable=SC2059 # fmt is the format passed to error()
printf "${fmt}" "${@}" >&2
}
fail() {
@ -145,6 +146,8 @@ check_user_validity() {
fail "invalid username '%s\n'" "${username}"
fi
# shellcheck disable=SC2086 # gid is a non-empty int
# shellcheck disable=SC2166 # [ .. -o .. ] works well in this case
if [ ${gid} -lt -2 -o ${gid} -eq 0 ]; then
fail "invalid gid '%d' for '%s'\n" ${gid} "${username}"
elif [ ${gid} -ge 0 ]; then
@ -171,6 +174,8 @@ check_user_validity() {
fi
fi
# shellcheck disable=SC2086 # uid is a non-empty int
# shellcheck disable=SC2166 # [ .. -o .. ] works well in this case
if [ ${uid} -lt -2 -o ${uid} -eq 0 ]; then
fail "invalid uid '%d' for '%s'\n" ${uid} "${username}"
elif [ ${uid} -ge 0 ]; then
@ -190,6 +195,7 @@ check_user_validity() {
fi
# check the user does not already exist in another group
# shellcheck disable=SC2166 # [ .. -a .. ] works well in this case
if [ -n "${_ugroup}" -a "${_ugroup}" != "${group}" ]; then
fail "user '%s' already exists with group '%s' (wants '%s')\n" \
"${username}" "${_ugroup}" "${group}"
@ -218,6 +224,7 @@ generate_gid() {
break
fi
done
# shellcheck disable=SC2086 # gid and maxgid are non-empty ints
if [ ${gid} -gt ${maxgid} ]; then
fail "can not allocate a GID for group '%s'\n" "${group}"
fi
@ -233,6 +240,7 @@ add_one_group() {
local members
# Generate a new GID if needed
# shellcheck disable=SC2086 # gid is a non-empty int
if [ ${gid} -eq ${AUTO_USER_ID} ]; then
gid="$( generate_gid "${group}" $FIRST_USER_GID $LAST_USER_GID )"
elif [ ${gid} -eq ${AUTO_SYSTEM_ID} ]; then
@ -272,6 +280,7 @@ generate_uid() {
break
fi
done
# shellcheck disable=SC2086 # uid is a non-empty int
if [ ${uid} -gt ${maxuid} ]; then
fail "can not allocate a UID for user '%s'\n" "${username}"
fi
@ -323,6 +332,7 @@ add_one_user() {
check_user_validity "${username}" "${uid}" "${group}" "${gid}"
# Generate a new UID if needed
# shellcheck disable=SC2086 # uid is a non-empty int
if [ ${uid} -eq ${AUTO_USER_ID} ]; then
uid="$( generate_uid "${username}" $FIRST_USER_UID $LAST_USER_UID )"
elif [ ${uid} -eq ${AUTO_SYSTEM_ID} ]; then
@ -399,7 +409,7 @@ main() {
fi
# Read in all the file in memory, exclude empty lines and comments
while read line; do
while read -r line; do
ENTRIES+=( "${line}" )
done < <( sed -r -e 's/#.*//; /^[[:space:]]*$/d;' "${USERS_TABLE}" )
@ -410,14 +420,16 @@ main() {
# First, create all the main groups which gid is *not* automatic
for line in "${ENTRIES[@]}"; do
read username uid group gid passwd home shell groups comment <<<"${line}"
read -r username uid group gid passwd home shell groups comment <<<"${line}"
# shellcheck disable=SC2086 # gid is a non-empty int
[ ${gid} -ge 0 ] || continue # Automatic gid
add_one_group "${group}" "${gid}"
done
# Then, create all the main groups which gid *is* automatic
for line in "${ENTRIES[@]}"; do
read username uid group gid passwd home shell groups comment <<<"${line}"
read -r username uid group gid passwd home shell groups comment <<<"${line}"
# shellcheck disable=SC2086 # gid is a non-empty int
[ ${gid} -lt 0 ] || continue # Non-automatic gid
add_one_group "${group}" "${gid}"
done
@ -428,8 +440,9 @@ main() {
# system gid if the uid is a system user (<= LAST_SYSTEM_UID),
# otherwise a user gid.
for line in "${ENTRIES[@]}"; do
read username uid group gid passwd home shell groups comment <<<"${line}"
read -r username uid group gid passwd home shell groups comment <<<"${line}"
if [ "${groups}" != "-" ]; then
# shellcheck disable=SC2086 # uid is a non-empty int
if [ ${uid} -le 0 ]; then
auto_id=${uid}
elif [ ${uid} -le ${LAST_SYSTEM_UID} ]; then
@ -450,8 +463,9 @@ main() {
# Now, add users whose uid is *not* automatic
for line in "${ENTRIES[@]}"; do
read username uid group gid passwd home shell groups comment <<<"${line}"
read -r username uid group gid passwd home shell groups comment <<<"${line}"
[ "${username}" != "-" ] || continue # Magic string to skip user creation
# shellcheck disable=SC2086 # uid is a non-empty int
[ ${uid} -ge 0 ] || continue # Automatic uid
add_one_user "${username}" "${uid}" "${group}" "${gid}" "${passwd}" \
"${home}" "${shell}" "${groups}" "${comment}"
@ -459,8 +473,9 @@ main() {
# Finally, add users whose uid *is* automatic
for line in "${ENTRIES[@]}"; do
read username uid group gid passwd home shell groups comment <<<"${line}"
read -r username uid group gid passwd home shell groups comment <<<"${line}"
[ "${username}" != "-" ] || continue # Magic string to skip user creation
# shellcheck disable=SC2086 # uid is a non-empty int
[ ${uid} -lt 0 ] || continue # Non-automatic uid
add_one_user "${username}" "${uid}" "${group}" "${gid}" "${passwd}" \
"${home}" "${shell}" "${groups}" "${comment}"