I'm trying to write a shell script which creates a user then ask the user which group name the user wants to assign the user which they have just created to and at the end it needs to ask if the user wants to create another user.
I keep getting a line 21: syntax error near unexpected token 'else' line 21' else'
#!/usr/bin/env bash
anotherUser() {
read -p "Do you want to add another user? [y/n] yn"
if [[ $yn = *[yY]* ]]; then
checkUser
fi
exit
}
checkUser() {
while :
do
read -p "Enter username you would like to generate: " userName
read -s -p "Enter password : " userPass
if id "$userName" >/dev/null; then
echo "Sorry user exists"
anotherUser
else
echo adduser "$userName"
printf "User %s has been added\n" "$userName"
else
read -p "Enter group you want to assign user to: " userGroups
useradd -G "userGroups" "$userName" &&
printf "User %s has been added\n" "$userName"
fi
break
done
exit
fi
done
}
checkUser