Shell Script to filter users and create them again from a back-up server

This is a script to filter the users out of etc/passwd and etc/group. So if you want to migrate of restore a server you can use this script from a backup to restore and make the same users on you had..

Please feedback and comments.

#!/bin/bash

prompt_list () {
# haal uit de argumenten de prompt en de keuzes
   ARRAY=("${@}")
   LEN=${#ARRAY[@]}
   PROMPT=${ARRAY[0]}
   CHOICES=${ARRAY[@]:1:$LEN}
#echo len: $LEN
#echo prompt: $PROMPT
#echo keuzes: $CHOICES
   echo -e "\e[01;31m${PROMPT}\e[00m"
   PS3="Maak je keuze ('s' voor stop): "
   CHOICE=false
   select ANSWER in ${CHOICES}; do
      case ${ANSWER} in
         *    ) CHOICE=${ANSWER};  break;;
      esac
   done
   check_stop ${CHOICE}
}

check_stop () {
   if [ -z ${1} ]
   then
      echo -e "\n\e[01;31mJe hebt er voor gekozen om te stoppen\e[00m\n"
      exit
   fi
}

contains() {
    string="$1"
    substring="$2"
    if test "${string#*$substring}" != "$string"
    then
        CONTAINS=ja
    else
        CONTAINS=nee
    fi
}

echo "--------------------------------------"
if [ $# = 0 ]
then
	echo "Usage: $0 <username>"
	exit
fi

	

USERS=$@ 
echo "Users zijn $USERS"
updatedb
prompt_list "Which tar.gz you want to use for userid?" `locate *etc*passwd.tar.gz`
PASSWD_OBJECT=${CHOICE}
echo "--------------------------------------"
prompt_list "Which tar.gz you want to use for groupid?" `locate *etc*group.tar.gz`
GROUP_OBJECT=${CHOICE}

tar -zxvf ${GROUP_OBJECT}
tar -zxvf ${PASSWD_OBJECT}

for USER in $USERS		
do		
	read -e -p "Which user you want to know the userid and groupid? " -i "$USER" USER
	USERID=`cat passwd | grep "^${USER}:" | cut -d":" -f3`
	if [ "${USERID}x" = "x" ]
	then
		echo "User $USER not in passwd"
	else
 	
		echo -e "userid is: \t$USERID"

		GROUPID=`cat passwd | grep "^${USER}:" | cut -d":" -f4`
		echo -e "groupid is: \t$GROUPID"
		
		HOMEID=`cat passwd | grep "^${USER}:" | cut -d":" -f6`
		echo -e "homedir is: \t$HOMEID"

		SHELLID=`cat passwd | grep "^${USER}:" | cut -d":" -f7`
		echo -e "shellid is: \t$SHELLID"

		GROUPID2=`cat group | grep "^${USER}:" | cut -d":" -f3`
                echo -e "groupid in group is: \t$GROUPID2"
	
		if [ ${GROUPID} -eq ${GROUPID2} ]
		then
			SUDOERS=`cat group | grep "^sudo:" | cut -d":" -f4`
                	echo -e "sudoers: \t$SUDOERS"
			
			contains $SUDOERS $USER
			echo $CONTAINS
			if [ "$CONTAINS" == "ja" ]
			then
				SUDOPARAM="-G sudo"
			else
    			        SUDOPARAM=""
			fi
		
			echo "This is ${SUDOPARAM}"

			groupadd -g ${GROUPID2} ${USER}
      			RESULT=$?
			echo -e "\e[01;31mResult Add to groep is ${RESULT}\e[00m"
			useradd -u ${USERID} -g ${GROUPID} ${SUDOPARAM} -d ${HOMEID} -m -s ${SHELLID} ${USER}
			RESULT=$?
			echo -e "\e[01;31mAdd result user is ${RESULT}\e[00m"
		else 
			echo "groupid of passwd is not equal from groupid of group"
		fi
	fi
done

What are you asking here? In general this script is too dependent on a specific environment. What if I don't have the bash shell or use sudo. Also, system administrators generally don't like to be prompted and "joe user" should not be running a script to add users so I would never hand this task off to someone not responsible for admin. There also seems to a language dependency here. Why is PS3 in a different language than the echo statements?
Not sure of the value of the functionality here. I don't mean to be negative but you asked for feedback.

Nice script.
We don't normal do this where I work. If the system is backed up, so are the user accounts and if you build the server again the accounts will be there from your last backup.

Run the following UUOC killer on your script:

sed 's/\<cat \([^ ][^ ]*  *\)| *\([^{(]\)/ < \1\2/' yourscript > newscript

and see the result with

diff yourscript newscript

:cool:

1 Like

... and even more effective were the single read

IFS=":" read X X USERID GROUPID X HOMEID SHELLID < <(grep $USER /etc/passwd)
2 Likes

Good point, this bashism (and zshism) simplifies a lot.
Of course keep the original grep "^${USER}:" !