From 4f2318ef5c45287feb5ec4248954107f599c8109 Mon Sep 17 00:00:00 2001 From: Aaron LI Date: Sun, 27 Dec 2020 21:41:57 +0800 Subject: Fix add_users() issue about '_gids' variable A pipe creates a subshell, so updates to the '_gids' variable would just get lost after the while loop. Use IO redirection instead of pipe to fix this issue. --- dfly-update | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) (limited to 'dfly-update') diff --git a/dfly-update b/dfly-update index e8d6db7..f4ab6ed 100755 --- a/dfly-update +++ b/dfly-update @@ -1,6 +1,6 @@ #!/bin/sh # -# Copyright (c) 2017-2019 Aaron LI +# Copyright (c) 2017-2020 Aaron LI # MIT License # # Tool to update a DragonFly BSD system using binary releases or @@ -265,36 +265,40 @@ add_users() { echo " => Adding new users ..." _gids="" - grep -Ev '^(#.*|\s*)$' ${fpasswd} | - while IFS=':' read -r _name _pw _uid _gid _class \ - _change _expire _gecos _home _shell; do + while IFS=':' read -r _name _pw _uid _gid _class \ + _change _expire _gecos _home _shell; do + case ${_name} in + '' | \#*) continue ;; + esac pw usershow ${_name} -q >/dev/null && continue # NOTE: There is circular dependence: 'groupadd' requires the members # already exist, while 'useradd' requires the group exists. # So first assign new users to the 'nogroup' group, and make # adjustments after group creation. - echo " * ${_name}, ${_uid}, ${_gid}, ${_gecos}, ${_home}, ${_shell}" + echo " * ${_name}: ${_uid}, ${_gid}, ${_gecos}, ${_home}, ${_shell}" pw useradd ${_name} \ -u ${_uid} -g nogroup -d ${_home} -s ${_shell} \ -L "${_class}" -c "${_gecos}" || exit ${EC_PW} _gids="${_gids} ${_name}:${_gid}" - done + done < ${fpasswd} echo " => Adding new groups ..." - grep -Ev '^(#.*|\s*)$' ${fgroup} | - while IFS=':' read -r _name _pw _gid _members; do + while IFS=':' read -r _name _pw _gid _members; do + case ${_name} in + '' | \#*) continue ;; + esac pw groupshow ${_name} -q >/dev/null && continue - echo " * ${_name}, ${_gid}, ${_members}" + echo " * ${_name}: ${_gid}, ${_members}" pw groupadd ${_name} -g ${_gid} -M "${_members}" || exit ${EC_PW} - done + done < ${fgroup} echo " => Adjusting the group of new users ..." for item in ${_gids}; do _name=${item%:*} _gid=${item#*:} - echo " * ${_name}, ${_gid}" + echo " * ${_name}: ${_gid}" pw usermod ${_name} -g ${_gid} || exit ${EC_PW} done } -- cgit v1.2.2