I have a useradd bash script which requests the user enter an e-mail address for the user being created. This is so the user receives his username/password in an e-mail when his/her account is created.
Currently this part of the code is very simple:
echo Enter the users e-mail address
read ADDRESS
What i'm finding is that sometimes when the operators run the script they are entereing blank information. How can I put a if statement in place that enforces they enter an e-mail address format.
I tried the following code but it doesn't work. The idea was to at least verify they are using the @ symbol.
string=@
if [[ $string != "@" ]] ; then
echo You have entered an invalid e-mail address!
exit 1
else
do something
I was able to get what I wanted using the following regex
echo Setting up e-mail notification!
echo Please take the time to ensure you are entering the correct e-mail information.
echo Enter the users e-mail address
read ADDRESS
if [[ "$ADDRESS" =~ "^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$" ]] ; then
getmail
else
echo You entered an invalid e-mail address!!!
cleanup
exit 1
fi