I need to write a bash script which will
- check if it is run as
root
user - ask for the user name
- check if user exists
- add new user with password
- ask for the group name
- check if this group exists
- add the user to the group
I wrote code like this
function create_user() {
if (( $EUID == 0)); then
echo "Only for root users."
exit
fi
echo "User name: "
read userschool
for x in $(awk -F":" '{ print }' /etc/passwd ); do
if [ $x == userschool ] ; then
echo "This user exists"
sleep $delay_time && exit
fi
done
sudo useradd -p $(openssl passwd -1 $PASS) $userschool
echo "User has been added"
sleep $delay_time
echo "Name of group"
read school
for x in $(awk -F":" '{ print }' /etc/group ); do
if [ $x == school ] ; then
echo "Group doesn't exists"
else
echo "$userschool has been added to group $school"
sleep $delay_time
break 2
fi
done
}
It almost works but if the user exists the script also adds him one more time and I don't know why. I want to communicate that the user exists and stop.