Ok, here's my 2 cents :)
Your input file must have the following layout:
fullname:loginname:usergroup:shelltouse
I used ':' as seperator instead of ' '. The fullname field can contain spaces (among other charachters), so that's why I choose ':'. So the following line is valid:
John Doe, Nowhereville (NL):stunned:users:/usr/bin/ksh
If no shelltouse is given _or_ the given shell is not valid, the default shell will be used and an error will be written to an errorfile.
If fullname and/or loginname and/or usergroup is missing the line will be ignored, an error will be written to an errorfile.
If loginname already exists the line will be ignored, an error will be written to an errorfile.
If given usergroup is not valid the line will be ignored, an error will be written to an errorfile.
If an error occurs the script will keep running, it will only ignore lines which have errors in them. There will also be an errorfile present if something went wrong.
You will be prompted before a user is added.
I did try to use the code you provided, but it got kinda fragmented. Key parts are used.
#!/bin/sh
#---------------------------------------------------------------------begin--#
# create_users <inputfile>
# Purpose: add users using an input file
# Layout inputfile: fullname:loginname:usergroup:shelltouse
#-----------------------------------------------------------
# Variables
DEFAULTSHELL="/bin/bash" # Change default shell here
LEGALSHELLS="/etc/shells" # File with valid shells
LEGALGROUPS="/etc/group" # File with valid groups
PWDIR="`pwd`"
EFILE="${PWDIR}/unable2create.`date '+%y%m%d'`"
# Functions
Check_All()
{
# is FULLNAME provided
[[ -z ${FULLNAME} ]] && ALL_OK="Full name"
# is LOGINNAME provided
[[ -z ${LOGINNAME} ]] && ALL_OK="Login name"
# is LOGINNAME valid
grep "^${LOGINNAME}:" /etc/passwd > /dev/null 2>&1
[ "$?" -eq 0 ] && ALL_OK="Loginname"
# is USERGROUP provided
[[ -z ${USERGROUP} ]] && ALL_OK="Usergroup"
# is USERGROUP valid
grep "^${USERGROUP}:" ${LEGALGROUPS} > /dev/null 2>&1
[ "$?" -eq 1 ] && ALL_OK="Usergroup"
}
# Main
clear
echo ""
echo "Running........"
echo ""
rm ${EFILE} > /dev/null 2>&1
# Is inputfile provided:
[[ -z $1 ]] && echo "Error: No input file given. Quiting." && exit 1
INFILE=$1
# Script run with root privilage:
if [ "$UID" != "0" ]
then
echo "Error: You must be root."
exit 1
fi
# Need the following to answer questions in a pipe
exec 3>&0
# Process inputfile:
cat ${INFILE} | \
while read ONELINE
do
FULLNAME="`echo ${ONELINE} | awk -F: '{ print $1 }'`"
LOGINNAME="`echo ${ONELINE} | awk -F: '{ print $2 }'`"
USERGROUP="`echo ${ONELINE} | awk -F: '{ print $3 }'`"
SHELLTOUSE="`echo ${ONELINE} | awk -F: '{ print $4 }'`"
# Check input fields
ALL_OK="0"
Check_All
if [[ ${ALL_OK} = "0" ]]
then
# All is well: Let's continue......
# is SHELLTOUSE provided
if [[ -z ${SHELLTOUSE} ]]
then
# Use default shell
SHELLTOUSE="${DEFAULTSHELL}"
else
# Is choosen shell valid
grep "^${SHELLTOUSE}$" ${LEGALSHELLS} > /dev/null 2>&1
if [ "$?" -eq 1 ]
then
# shell not valid, use default instead.
SHELLTOUSE="${DEFAULTSHELL}"
echo "Shell-> ${ONELINE}" >> ${EFILE}
fi
fi
# Show and ask
echo "Name : $FULLNAME"
echo "Login : $LOGINNAME"
echo "Group : $USERGROUP"
echo "Shell : $SHELLTOUSE"
echo ""
printf "%s\n" "Is all this correct?"
<&3 read ANSWER
case "$ANSWER" in
y | Y | Yes | YES )
$(adduser "$LOGINNAME" -c "$FULLNAME" -s "$SHELLTOUSE" -g "$USERGROUP")
<&3 /usr/bin/passwd "$LOGINNAME"
echo ""
;;
n | N | No | NO )
echo ""
echo "Ignoring this line of input, will continue with next line."
;;
esac
else
# Something went wrong: Write to errorfile, set token and continue
echo "${ALL_OK}-> ${ONELINE}" >> ${EFILE}
fi
done
# Did something go wrong
if [[ -a ${EFILE} ]]
then
# Yep
echo ""
echo "-----------------------------------------------------------------------"
echo "Something went wrong during processing, please check:"
echo ""
echo "${EFILE}"
fi
# Done
echo ""
echo "----------------------------------------------------------------------------"
echo "Done."
echo ""
exit 0
#-----------------------------------------------------------------------end--#